我正在尝试通过SymSpell实现自动更正

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

该词典包含一个dictionaryItem对象,该对象需要序列化才能由NSCoder保存

我试图将方法添加到对象中,但出现错误“无法将init(coder adecoder nscoder)swift发送到NSCoder类的抽象对象”

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")
     }
}

有什么想法如何解决?

最佳答案

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()
    }
}

关于ios - nscoder swift无法发送到NSCoder类的抽象对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36529246/

10-09 15:45