NSCalendarUnit
是一個無符號長,所以使用上NSNumber
兩種方法用於無符號多頭可以「盒裝」(製作成一個對象):
NSCalendarUnit someNSCalendarUnit = NSCalendarUnitDay;
NSNumber *boxed = [NSNumber numberWithUnsignedLong:someNSCalendarUnit];
和
NSCalendarUnit unboxed = [boxed unsignedLongLongValue];
// now, unboxed == someNSCalendarUnit
而且你可能知道如何編碼一個解碼NSNumbers,只需在編碼/解碼方法中添加額外的盒步...
- (void)encodeWithCoder:(NSCoder*)encoder {
[super encodeWithCoder:encoder];
NSNumber *boxed = [NSNumber numberWithUnsignedLong:self.someNSCalendarUnit];
[encoder encodeObject:boxed forKey:@"someNSCalendarUnit"];
// ...
}
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSNumber *boxed = [aDecoder decodeObjectForKey:@"someNSCalendarUnit"];
_someNSCalendarUnit = [boxed unsignedLongLongValue];
// ...
}
return self;
}