Skip to content

Instantly share code, notes, and snippets.

@swillits
Created January 15, 2023 22:12
Show Gist options
  • Save swillits/ea4ddebc5abd230600db1354f389095b to your computer and use it in GitHub Desktop.
Save swillits/ea4ddebc5abd230600db1354f389095b to your computer and use it in GitHub Desktop.
// Test 2 failed
// Error Domain=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver decodeObjectForKey:]:
// value for key (NSAttributes) contains too many nested (NSDictionary)s"
// UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver decodeObjectForKey:]: value for
// key (NSAttributes) contains too many nested (NSDictionary)s}
// Test 3 succeeded
// Finished
import Cocoa
class MyObj: NSObject, NSSecureCoding {
class var supportsSecureCoding: Bool {
return true
}
func encode(with coder: NSCoder) {
let attrStr = NSAttributedString(string: "text", attributes: [:])
coder.encode(attrStr, forKey: "attributedString")
}
required init?(coder: NSCoder) {
let attrStr = coder.decodeObject(of: NSAttributedString.self, forKey: "attributedString")
precondition(attrStr != nil)
}
override init() {
}
}
// Archiving a single instance works fine
do {
let data = try! NSKeyedArchiver.archivedData(withRootObject: MyObj(), requiringSecureCoding: true)
let _ = try NSKeyedUnarchiver.unarchivedObject(ofClass: MyObj.self, from: data)
} catch let err {
print("Test 1 failed")
print(err)
}
// Archiving an array of instances fails
do {
let data = try! NSKeyedArchiver.archivedData(withRootObject: [MyObj()], requiringSecureCoding: true)
let _ = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: MyObj.self, from: data)
} catch let err {
print("Test 2 failed")
print(err)
}
// Doing it with regular NSCoding works fine
do {
let data = try! NSKeyedArchiver.archivedData(withRootObject: [MyObj()])
let root = NSKeyedUnarchiver.unarchiveObject(with: data)
precondition(root != nil)
print("Test 3 succeeded")
}
print("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment