nscoder swift无法发送到类NSCoder的抽象对象

时间:2016-04-10 11:41:20

标签: ios swift custom-keyboard

我试图通过SymSpell

实现自动更正

我已在容器应用中创建了字典,应将其保存并从键盘扩展程序中读取

字典包含一个required init(coder aDecoder: NSCoder) { if let suggestions = aDecoder.decodeObjectForKey("suggestions") as? [Int] { self.suggestions = suggestions } if let count = aDecoder.decodeObjectForKey("count") as? Int { self.count = count } } func encodeWithCoder(aCoder: NSCoder) { if let count = self.count as? Int { aCoder.encodeObject(count, forKey: "count") } if let suggestions = self.suggestions as? [Int] { aCoder.encodeObject(suggestions, forKey: "suggestions") } } 对象,需要序列化才能由NSCoder保存

我尝试将方法添加到对象但是我收到错误" init(编码器编码器nscoder)swift无法发送到类NSCoder的抽象对象"

{{1}}

有任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

import Foundation

class SuggestionModel: NSObject, NSCoding {
    var suggestions: [Int]?
    var count : Int?

    required init(coder aDecoder: NSCoder) {
        self.suggestions = aDecoder.decodeObjectForKey("suggestions") as? [Int]
        self.count = aDecoder.decodeObjectForKey("count") as? Int
        super.init()
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(self.count, forKey: "count")
        aCoder.encodeObject(self.suggestions, forKey: "suggestions")
    }

    override init() {
        super.init()
    }
}