我有一个类Person,它被多次实例化.每个人都得到自己的计时器.当我在init为Person我打电话startTimer().
class Person {
var timer = NSTimer()
func startTimer() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerTick"), userInfo: nil, repeats: true)
}
func timerTick() {
angerLevel++
println("Angry! \(angerLevel)")
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
所以我可能在一个数组中有3个Person实例Person[].我收到一个错误:
2014-06-25 13:57:14.956 ThisProgram[3842:148856] *** NSForwarding: warning: object 0x113760048 of class '_TtC11ThisProgram6Person' does not implement methodSignatureForSelector: -- trouble ahead
Run Code Online (Sandbox Code Playgroud)
我在其他地方读过我应该继承的,NSObject但这是在Swift而不是Obj-C.该功能在课堂内,所以我不知道该怎么做.
在decodeDouble上NSCoder返回一个非可选值,但我想,以确定一个值是否是零它编码之前.
这是我的情景:
var optionalDouble: Double? = nil
func encode(with aCoder: NSCoder) {
if let optionalDouble {
aCoder.encode(optionalDouble, forKey: "myOptionalDouble")
}
}
convenience required init?(coder aDecoder: NSCoder) {
optionalDouble = aDecoder.decodeDouble(forKey: "myOptionalDouble")
// here optionalDouble is never nil anymore
}
Run Code Online (Sandbox Code Playgroud)
因此0,如果从未设置值,则解码double会返回,因此我似乎无法确定值是实际0还是nil在编码之前
有没有办法在编码之前检查双精度是否为零?