【发布时间】:2020-01-02 18:25:03
【问题描述】:
我正在尝试保存一些开关的状态,并在应用再次启动时恢复它,但当我写这个时:
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
coder.encode(Int32(self.runSwitch1.state.rawValue), forKey: CodingKey.Protocol)
}
我收到了这个错误:
无法将“CodingKey.Protocol.Type”类型的值转换为预期的参数类型“String”
我该怎么办?
【问题讨论】:
-
使用 UserDefaults 存储值..
-
forKey后面的参数必须是String,这就是错误告诉你的。
标签: swift encoding decoding state-restoration