最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]NSKeyedArchiver.archiveRootObject出错:class does not implement methodSignatureForSelector

Swift crifan 2209浏览 0评论

要将一个对象压缩保存:

代码:

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
20 queries in 0.096 seconds, using 9.32MB memory