首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nscoder swift不能发送到NSCoder类的抽象对象

nscoder swift不能发送到NSCoder类的抽象对象
EN

Stack Overflow用户
提问于 2016-04-10 19:41:20
回答 1查看 2.7K关注 0票数 2

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

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

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

我试图将方法添加到对象中,但得到了一个错误"init(coder adecoder nscoder) swift cannot be sent to a abstract object of class NSCoder“

代码语言:javascript
复制
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")
     }
}

有什么想法可以解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2016-04-10 23:01:32

代码语言:javascript
复制
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()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36529246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档