要将一个对象压缩保存:
代码:
class ContactSectionItem{ var indexTitle:String var contactItemList:[ContactItem] init(){ indexTitle = "" contactItemList = [ContactItem]() } } var contactSectionItemList:[ContactSectionItem] let saveOk = NSKeyedArchiver.archiveRootObject(contactSectionItemList, toFile: saveFilePath) |
出错了:
NSForwarding: warning: object 0x7b090680 of class ‘JianDao.ContactSectionItem’ does not implement methodSignatureForSelector: — trouble ahead Unrecognized selector -[JianDao.ContactSectionItem replacementObjectForKeyedArchiver:] |
解决办法很简单:
将对应的对象ContactSectionItem去实现对应的NSCoding:
private let ContactSectionItem_indexTitle = "ContactSectionItem_indexTitle" private let ContactSectionItem_contactItemList = "ContactSectionItem_contactItemList" class ContactSectionItem:NSObject, NSCoding { var indexTitle:String var contactItemList:[ContactItem] override init(){ indexTitle = "" contactItemList = [ContactItem]() super.init() } // MARK: NSCoding required init?(coder aDecoder: NSCoder){ self.indexTitle = aDecoder.decodeObjectForKey(ContactSectionItem_indexTitle) as! String self.contactItemList = aDecoder.decodeObjectForKey(ContactSectionItem_contactItemList) as! [ContactItem] super.init() } func encodeWithCoder(aCoder: NSCoder){ aCoder.encodeObject(indexTitle, forKey: ContactSectionItem_indexTitle) aCoder.encodeObject(contactItemList, forKey: ContactSectionItem_contactItemList) } } |
即可。
当然,其中的ContactItem是之前就已经实现了NSCoding:
class ContactItem:NSObject, NSCoding { |
转载请注明:在路上 » [已解决]NSKeyedArchiver.archiveRootObject出错:class does not implement methodSignatureForSelector