1

我得到错误:

[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)

    }
}
4

0 回答 0