Tag: nscoding

有无法识别selector -replacementObjectForKeyedArchiver:在Swift中实现NSCoding时崩溃

我创build了一个符合NSCoding的Swift类。 (Xcode 6 GM,Swift 1.0) import Foundation private var nextNonce = 1000 class Command: NSCoding { let nonce: Int let string: String! init(string: String) { self.nonce = nextNonce++ self.string = string } required init(coder aDecoder: NSCoder) { nonce = aDecoder.decodeIntegerForKey("nonce") string = aDecoder.decodeObjectForKey("string") as String } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeInteger(nonce, forKey: "nonce") aCoder.encodeObject(string, forKey: […]

Swift 3保存并从userDefaults中检索自定义对象

我在Playground中使用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 ) } […]

为什么NSUserDefaults无法在iPhone SDK中保存NSMutableDictionary?

我想在NSUserDefaults保存一个NSMutableDictionary对象。 NSMutableDictionary的键types是NSString ,值types是NSArray ,它包含一个实现NSCoding的对象列表。 每个文档, NSString和NSArray都符合NSCoding 。 我得到这个错误: [NSUserDefaults setObject:forKey:]:尝试插入NSCFDictionary类的非属性值…. 任何解决scheme?

将具有NSCoding的自定义SWIFT类保存到UserDefaults

我目前正试图保存一个自定义的Swift类到NSUserDefaults。 这里是我的游乐场的代码: 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.blogName […]