Created
March 20, 2021 20:12
-
-
Save christianselig/9e94525c649510bc0066e00d53fae6af to your computer and use it in GitHub Desktop.
Zombie banana uprising
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Banana: NSObject, NSSecureCoding { | |
let bananaName: String | |
let userInfo: [String: Any] | |
static var supportsSecureCoding: Bool = true | |
init(bananaName: String, userInfo: [String: Any]) { | |
self.bananaName = bananaName | |
self.userInfo = userInfo | |
} | |
required init?(coder: NSCoder) { | |
guard | |
let bananaName = coder.decodeObject(of: NSString.self, forKey: "bananaName") as String?, | |
let userInfo = coder.decodeObject(of: NSDictionary.self, forKey: "userInfo") as? [String: Any] | |
else { | |
return nil | |
} | |
self.bananaName = bananaName | |
self.userInfo = userInfo | |
} | |
func encode(with coder: NSCoder) { | |
coder.encode(self.bananaName, forKey: "bananaName") | |
coder.encode(self.userInfo, forKey: "userInfo") | |
} | |
} | |
func testing() { | |
let subordinateBananas: [Banana] = [Banana(bananaName: "Douglas", userInfo: [:]), Banana(bananaName: "Phil", userInfo: [:])] | |
let leadBanana = Banana(bananaName: "Penny", userInfo: ["subordinates": subordinateBananas]) | |
let toEncode: [String: Any] = ["lead": leadBanana] | |
let encodedData = try! NSKeyedArchiver.archivedData(withRootObject: toEncode, requiringSecureCoding: true) | |
// β CRASH π₯ | |
let resurrectedBananaSquad = try! NSKeyedUnarchiver.unarchivedObject(ofClasses: [NSDictionary.self, NSArray.self, Banana.self], from: encodedData) | |
print("ππ§ββοΈπ \(String(describing: resurrectedBananaSquad))") | |
} |
@KhaosT is spot on, for this Gist you also need to keep the cast:
let userInfo = coder.decodeObject(of: [NSDictionary.self, NSArray.self, Banana.self], forKey: "userInfo") as? [String: Any]
Y'all the best
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is with
You need