gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:36:43 28 4
gpt4 key购买 nike

我正在尝试通过 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")
}
}

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

最佳答案

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com