我有一个对象,其属性可能为零.我应该如何在encodeWithCoder(和decodeWithCoder)中实现它?
- (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_duration forKey:kDuration]; //_duration could be nil }
解决方法
- (void)encodeWithCoder:(NSCoder *)aCoder { if (_duration) { [aCoder encodeObject:_duration forKey:kDuration]; //_duration could be nil } }
(如果密钥不存在,您的对象将在解码时为零.)