[NSKeyedUnarchiver init]:尝试保存自定义类时无法使用-init进行初始化'错误

时间:2016-02-05 20:44:45

标签: swift load nskeyedunarchiver

我收到错误:

  

[NSKeyedUnarchiver init]:不能使用-init进行初始化   在尝试加载自定义类时。

继承我的班级初学者:

   required init?(coder aDecoder: NSCoder) {
        print("intializing")
        if let quoteName = aDecoder.decodeObjectForKey("quoteName") as? String {
            self._quoteName = quoteName
        }
        if let quote = aDecoder.decodeObjectForKey("quote") as? String {
            self._quote = quote
        }
        if let soundFileName = aDecoder.decodeObjectForKey("soundFileName") as? String {
            self._soundFileName  = soundFileName
        }
        if let soundFileType = aDecoder.decodeObjectForKey("soundFileType") as? String {
            self._soundFileType = soundFileType
        }
        if let audioFilePath = aDecoder.decodeObjectForKey("audioFilePath") as? String {
            self._soundFileType = audioFilePath
        }
        if let state = aDecoder.decodeObjectForKey("state") as? Bool {
            self._state  = state
        }
        if let number = aDecoder.decodeObjectForKey("number") as? Int {
            self._number = number
        }

    }

继承我的编码器功能

  func encodeWithCoder(aCoder: NSCoder) {
            aCoder.encodeObject(self._quoteName, forKey: "quoteName")
            aCoder.encodeObject(self._quote, forKey: "quote")
            aCoder.encodeObject(self._soundFileType, forKey: "soundFileName")
            aCoder.encodeObject(self._soundFileType, forKey: "soundFileType")
            aCoder.encodeObject(self._audioFilePath, forKey: "audioFilePath")
            aCoder.encodeObject(self._state, forKey: "state")
            aCoder.encodeObject(self._number, forKey: "number")
    }

最后我要求加载:

    class func loadQuoteList(){
    print("loading Quotes")
    quoteList.removeAll()
    var quoteListLength = defaults.integerForKey("quoteListLength")

    //unarchives Quote Object
    var unarc = NSKeyedUnarchiver()
    if let data = defaults.objectForKey("Quote") as? NSData {
        unarc = NSKeyedUnarchiver(forReadingWithData: data)
    }

    print("loading individual quotes")
    for(var index = 0; index < quoteListLength; index++){
        var newQuote = unarc.decodeObjectForKey("Quote\(index)") as! Quote
        quoteList[index] = newQuote
        print(newQuote._quoteName)

    }
}

0 个答案:

没有答案