nscoding

Swift 3 saving and retrieving custom object from userDefaults

雨燕双飞 提交于 2019-11-26 11:40:35
I have this in Playground using Swift 3, Xcode 8.0: import Foundation class Person: NSObject, NSCoding { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } required convenience init(coder aDecoder: NSCoder) { let name = aDecoder.decodeObject(forKey: "name") as! String let age = aDecoder.decodeObject(forKey: "age") as! Int self.init( name: name, age: age ) } func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: "name") aCoder.encode(age, forKey: "age") } } create array of Person let newPerson = Person(name: "Joe", age: 10) var people =

Attempt to insert non-property list object when trying to save a custom object in Swift 3

本小妞迷上赌 提交于 2019-11-26 06:36:54
问题 I have a simple object which conforms to the NSCoding protocol. import Foundation class JobCategory: NSObject, NSCoding { var id: Int var name: String var URLString: String init(id: Int, name: String, URLString: String) { self.id = id self.name = name self.URLString = URLString } // MARK: - NSCoding required init(coder aDecoder: NSCoder) { id = aDecoder.decodeObject(forKey: \"id\") as? Int ?? aDecoder.decodeInteger(forKey: \"id\") name = aDecoder.decodeObject(forKey: \"name\") as! String

Swift 3 saving and retrieving custom object from userDefaults

左心房为你撑大大i 提交于 2019-11-26 03:29:47
问题 I have this in Playground using Swift 3, Xcode 8.0: import Foundation class Person: NSObject, NSCoding { var name: String var age: Int init(name: String, age: Int) { self.name = name self.age = age } required convenience init(coder aDecoder: NSCoder) { let name = aDecoder.decodeObject(forKey: \"name\") as! String let age = aDecoder.decodeObject(forKey: \"age\") as! Int self.init( name: name, age: age ) } func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: \"name\") aCoder.encode

Why NSUserDefaults failed to save NSMutableDictionary in iOS?

三世轮回 提交于 2019-11-26 02:55:37
I'd like to save an NSMutableDictionary object in NSUserDefaults . The key type in NSMutableDictionary is NSString , the value type is NSArray , which contains a list of object which implements NSCoding . Per document, NSString and NSArray both are conform to NSCoding . I am getting this error: [NSUserDefaults setObject: forKey:]: Attempt to insert non-property value.... of class NSCFDictionary. BlueDolphin I found out one alternative, before save, I encode the root object ( NSArray object) using NSKeyedArchiver , which ends with NSData . Then use UserDefaults save the NSData . When I need the

Why NSUserDefaults failed to save NSMutableDictionary in iOS?

时间秒杀一切 提交于 2019-11-26 01:12:18
问题 I\'d like to save an NSMutableDictionary object in NSUserDefaults . The key type in NSMutableDictionary is NSString , the value type is NSArray , which contains a list of object which implements NSCoding . Per document, NSString and NSArray both are conform to NSCoding . I am getting this error: [NSUserDefaults setObject: forKey:]: Attempt to insert non-property value.... of class NSCFDictionary. 回答1: I found out one alternative, before save, I encode the root object ( NSArray object) using

Saving custom Swift class with NSCoding to UserDefaults

房东的猫 提交于 2019-11-26 00:36:51
问题 I am currently trying to save a custom Swift class to NSUserDefaults. Here is the code from my Playground: import Foundation class Blog : NSObject, NSCoding { var blogName: String? override init() {} required init(coder aDecoder: NSCoder) { if let blogName = aDecoder.decodeObjectForKey(\"blogName\") as? String { self.blogName = blogName } } func encodeWithCoder(aCoder: NSCoder) { if let blogName = self.blogName { aCoder.encodeObject(blogName, forKey: \"blogName\") } } } var blog = Blog() blog