I am having trouble saving a Bool variable using the NSSecureCoding within a Swift app.
I don't have any experience with Objective-C and I'm relatively new to Swift (I have a c# background). As I understand, using NSSecureCoding requires us to use the string and int counterparts in Objective-C - i.e. NSString and NSNumber. I was able to successfully encode and decode ints and strings this way:
// Encode
coder.encode(myString as NSString, forKey: PropertyKey.myStrKey)
coder.encode(NSNumber(value: myInt), forKey: PropertyKey.myIntKey)
// Decode
let myString = coder.decodeObject(of: NSString.self, forKey: PropertyKey.myStrKey) as String? ?? ""
let myInt = coder.decodeObject(of: NSNumber.self, forKey: PropertyKey.myIntKey)
However, I am not sure how to handle booleans. I tried this:
// Encode
coder.encode(NSNumber(value: myBool), forKey: PropertyKey.myBoolKey)
// Decode
let myBool = coder.decodeObject(of: NSNumber.self, forKey: PropertyKey.myBoolKey)
print("\(String(describing: myBool))")
But this always prints: Optional(1)
irrespective of the initial value of myBool.
Any help would be greatly appreciated. Thanks.
No need to encode a
String
and or aNSNumber
. You can simply encode yourBool
and just make sure to use NSCoder's decodeBool method when decoding it.Playground testing: