最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 3183浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.215 seconds, using 22.09MB memory