NSCodable
A Swift package that bridges NSKeyedUnarchiver with Swift's Codable system, allowing you to decode data archived with NSKeyedArchiver into Swift Codable types.
Overview
NSDecoder is a custom decoder that implements Swift's Decoder protocol, enabling you to decode NSKeyedArchiver data into any Swift type that conforms to Codable.
Features
- ✅ Decodes data from
NSKeyedArchiver into Codable types
- ✅ Supports all primitive types (Bool, Int, Float, Double, String, etc.)
- ✅ Supports nested structures with
NSCoderBridge
- ✅ Supports arrays, including nested arrays (e.g.,
[[Int]], [[[String]]])
- ✅ Supports sets of primitives and hashable types
- ✅ Supports dictionaries with arbitrary nesting (e.g.,
[String: [Int]], [String: [String: T]])
- ✅ Supports complex nested collections (arrays of dictionaries, dictionaries of arrays, etc.)
- ✅ Supports legacy NSCoding object mapping with complex nested structures
- ✅ Supports keyed and unkeyed containers
- ✅ Supports custom decoder user info
- ✅ Full Swift 6.1 compatibility with strict concurrency
Installation
Add this package to your Package.swift file:
dependencies: [
.package(url: "https://github.com/yourusername/NSCodable.git", from: "1.0.0")
]
Usage
Basic Usage
import Foundation
import NSCodable
// Define your Codable type
struct Person: Codable {
let name: String
let age: Int
let email: String
}
// Create archived data using NSKeyedArchiver
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
archiver.encode("John Doe", forKey: "name")
archiver.encode(30, forKey: "age")
archiver.encode("john@example.com", forKey: "email")
archiver.finishEncoding()
let data = archiver.encodedData
// Decode using NSDecoder
let decoder = NSDecoder()
decoder.options.rootKey = nil
let person = try decoder.decode(Person.self, from: data)
print(person.name) // "John Doe"
print(person.age) // 30
Decoding Legacy NSCoding Objects
You can decode data from legacy NSCoding objects into modern Codable structs using the mappedClasses option:
// Legacy NSCoding class
@objc(LegacyLocation)
class LegacyLocation: NSObject, NSCoding {
let latitude: Double
let longitude: Double
let name: String
init(latitude: Double, longitude: Double, name: String) {
self.latitude = latitude
self.longitude = longitude
self.name = name
super.init()
}
required init?(coder: NSCoder) {
self.latitude = coder.decodeDouble(forKey: "latitude")
self.longitude = coder.decodeDouble(forKey: "longitude")
self.name = coder.decodeObject(forKey: "name") as? String ?? ""
super.init()
}
func encode(with coder: NSCoder) {
coder.encode(latitude, forKey: "latitude")
coder.encode(longitude, forKey: "longitude")
coder.encode(name, forKey: "name")
}
}
// Modern Codable struct
struct Location: Codable {
let latitude: Double
let longitude: Double
let name: String
}
// Create and archive legacy object
let legacyLocation = LegacyLocation(latitude: 37.7749, longitude: -122.4194, name: "SF")
let data = try NSKeyedArchiver.archivedData(
withRootObject: legacyLocation,
requiringSecureCoding: false
)
// Decode into modern Codable struct - map the legacy class
let options = NSDecoder.Options(mappedClasses: ["LegacyLocation"])
let decoder = NSDecoder(options: options)
let modernLocation = try decoder.decode(Location.self, from: data)
Nested NSCoding Objects
The decoder automatically handles nested NSCoding objects through NSCoderBridge. You can nest structures arbitrarily deep:
@objc(LegacyAddress)
class LegacyAddress: NSObject, NSCoding {
let street: String
let city: String
// ... NSCoding implementation
}
@objc(LegacyPerson)
class LegacyPerson: NSObject, NSCoding {
let name: String
let address: LegacyAddress // Nested NSCoding object
let tags: [String] // Arrays work too
// ... NSCoding implementation
}
// Modern structs
struct Address: Codable {
let street: String
let city: String
}
struct Person: Codable {
let name: String
let address: Address
let tags: [String]
}
// Decode with mapped classes
let options = NSDecoder.Options(mappedClasses: Set([
"LegacyPerson",
"LegacyAddress"
]))
let decoder = NSDecoder(options: options)
let modern = try decoder.decode(Person.self, from: legacyData)
Decoder Options
NSDecoder supports configuration through the Options type:
// Create options with mapped classes
var options = NSDecoder.Options()
options.mappedClasses.insert("LegacyProduct")
// Initialize decoder with options
let decoder = NSDecoder(options: options)
// Or modify options after initialization
let decoder = NSDecoder()
decoder.options.mappedClasses.insert("MyClass")
Options Properties
-
mappedClasses: A Set<String> containing legacy NSCoding class names that should be mapped to Codable types during decoding. Add the class names (from @objc(ClassName) or the Swift class name) to enable automatic bridging through NSCoderBridge.
-
rootKey: An optional String specifying the key for the root object in the archive (defaults to NSKeyedArchiveRootObjectKey). Set to nil to decode directly without expecting a root key wrapper.
User Info
You can pass custom information to your decoder using the userInfo dictionary: