代码
class Model : NSObject,NSCoding { var seq: NSNumber? var seq2: Int? // problem with seq2,NSInteger is not ok,either var ID: String? var value: String? overrIDe init() { super.init() } required init?(coder aDecoder: NSCoder){ self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber self.seq2 = aDecoder.decodeInteger(forKey: "seq2") self.ID = aDecoder.decodeObject(forKey: "ID") as? String self.value = aDecoder.decodeObject(forKey: "value") as? String } func encode(with aCoder: NSCoder){ aCoder.encode(seq,forKey: "seq") aCoder.encode(seq2,forKey: "seq2") aCoder.encode(ID,forKey: "ID") aCoder.encode(value,forKey: "value") }}解决方法 问题是seq2不是Int,而是Int?可选的.它不能表示为Objective-C整数.
您可以使用decodeObject:
required init?(coder aDecoder: NSCoder){ self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber self.seq2 = aDecoder.decodeObject(forKey: "seq2") as? Int self.ID = aDecoder.decodeObject(forKey: "ID") as? String self.value = aDecoder.decodeObject(forKey: "value") as? String super.init()}
或更改它,使其不是可选的:
class Model : NSObject,NSCoding { var seq: NSNumber? var seq2: Int var ID: String? var value: String? init(seq: NSNumber,seq2: Int,ID: String,value: String) { self.seq = seq self.seq2 = seq2 self.ID = ID self.value = value super.init() } required init?(coder aDecoder: NSCoder) { self.seq = aDecoder.decodeObject(forKey: "seq") as? NSNumber self.seq2 = aDecoder.decodeInteger(forKey: "seq2") self.ID = aDecoder.decodeObject(forKey: "ID") as? String self.value = aDecoder.decodeObject(forKey: "value") as? String super.init() } func encode(with aCoder: NSCoder) { aCoder.encode(seq,forKey: "value") } overrIDe var description: String { return "<Model; seq=\(seq); seq2=\(seq2); ID=\(ID); value=\(value)>" }}总结
以上是内存溢出为你收集整理的objective-c – 无法在Swift中使用NSCoder解码Int全部内容,希望文章能够帮你解决objective-c – 无法在Swift中使用NSCoder解码Int所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)