I have a instance variable name in String
var name: String
My class implements the NSCoding protocol. So for name I had
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.name, forKey: kName)
}
required init(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObjectForKey(kName) as String // CRASH HERE
}
Result? I was getting a run time crash during initiation with decoder. I changed init to this:
var temp = aDecoder.decodeObjectForKey(kName) as NSString!
self.name = aDecoder.decodeObjectForKey(kName) as String
and realised the value temp is holding the right NSString value. so I thought the line below is going to fix it but it issues a linker error:
self.name = aDecoder.decodeObjectForKey(kName) as NSString!
the questions is how to take the temp and put it into name?
self.name = aDecoder.decodeObjectForKey(kName) as AnyObject! as String
– Eduard