[英]How to initialize a custom class instance that conforms to NSCoding?
I'm trying to use NSKeyedArchiver to store custom class instance. 我正在尝试使用NSKeyedArchiver存储自定义类实例。
class feedBack: NSObject, NSCoding {
var choiceA = 0
var choiceB = 0
var choiceC = 0
var choiceD = 0
var choiceNULL = 0
var sheetName = ""
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.choiceA, forKey: "choiceA")
aCoder.encodeObject(self.choiceB, forKey: "choiceB")
aCoder.encodeObject(self.choiceC, forKey: "choiceC")
aCoder.encodeObject(self.choiceD, forKey: "choiceD")
aCoder.encodeObject(self.choiceNULL, forKey: "choiceNULL")
aCoder.encodeObject(self.sheetName, forKey: "sheetName")
}
required init(coder aDecoder: NSCoder) {
self.choiceA = aDecoder.decodeObjectForKey("choiceA") as! Int
self.choiceB = aDecoder.decodeObjectForKey("choiceB") as! Int
self.choiceC = aDecoder.decodeObjectForKey("choiceC") as! Int
self.choiceD = aDecoder.decodeObjectForKey("choiceD") as! Int
self.choiceNULL = aDecoder.decodeObjectForKey("choiceNULL") as! Int
self.sheetName = aDecoder.decodeObjectForKey("sheetName") as! String
}
}
Before feedBack
conforms to NSCoding, I use var fb = feedBack()
to create a new instance of feedBack. 在
feedBack
符合NSCoding之前,我使用var fb = feedBack()
创建feedBack的新实例。 Now the compiler throws Missing argument for parameter coder in call
error. 现在,编译
Missing argument for parameter coder in call
错误中Missing argument for parameter coder in call
器抛出Missing argument for parameter coder in call
。
Since the initWithCoder
is required, how do I call the previous initializer with no parameter? 由于
initWithCoder
是必需的,我如何不带参数调用以前的初始化程序?
Just override init() and that should do it. 只需重写init()即可。
class feedBack: NSObject, NSCoding {
var choiceA = 0
var choiceB = 0
var choiceC = 0
var choiceD = 0
var choiceNULL = 0
var sheetName = ""
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.choiceA, forKey: "choiceA")
aCoder.encodeObject(self.choiceB, forKey: "choiceB")
aCoder.encodeObject(self.choiceC, forKey: "choiceC")
aCoder.encodeObject(self.choiceD, forKey: "choiceD")
aCoder.encodeObject(self.choiceNULL, forKey: "choiceNULL")
aCoder.encodeObject(self.sheetName, forKey: "sheetName")
}
required init(coder aDecoder: NSCoder) {
self.choiceA = aDecoder.decodeObjectForKey("choiceA") as! Int
self.choiceB = aDecoder.decodeObjectForKey("choiceB") as! Int
self.choiceC = aDecoder.decodeObjectForKey("choiceC") as! Int
self.choiceD = aDecoder.decodeObjectForKey("choiceD") as! Int
self.choiceNULL = aDecoder.decodeObjectForKey("choiceNULL") as! Int
self.sheetName = aDecoder.decodeObjectForKey("sheetName") as! String
}
override init(){
}
}
feedBack()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.