【问题标题】:nscoder swift cannot be sent to an abstract object of class NSCodernscoder swift 无法发送到 NSCoder 类的抽象对象
【发布时间】:2016-07-31 11:12:42
【问题描述】:

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

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

字典包含一个dictionaryItem对象,需要被NSCoder序列化才能保存

我尝试将方法添加到对象,但出现错误“init(coder adecoder nscoder) swift cannot be sent to an abstract object of class 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")
     }
}

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

【问题讨论】:

    标签: ios swift custom-keyboard


    【解决方案1】:
    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()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2013-08-22
      • 2017-11-13
      • 1970-01-01
      相关资源
      最近更新 更多