我正在使用此代码对NSCalenderUnit
- (void)encodeWithCoder:(NSCoder*)encoder {
[super encodeWithCoder:encoder];
NSNumber *boxed = [NSNumber numberWithUnsignedLong:self.myNSCalendarUnit];
[encoder encodeObject:boxed forKey:@"myNSCalendarUnit"];
// ...
}
- (id)initWithCoder:(NSCoder*)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
NSNumber *boxed = [aDecoder decodeObjectForKey:@"myNSCalendarUnit"];
_myNSCalendarUnit = [boxed unsignedLongLongValue];
// ...
}
return self;
}
但是我收到了这个警告
隐式转换失去整数精度:'unsigned long long'to 'NSCalendarUnit'(又名'enum NSCalendarUnit')
如何更正代码以删除此警告?
答案 0 :(得分:0)
正如您可以看到NSCalendarUnit的描述类型
typedef NS_OPTIONS(NSUInteger, NSCalendarUnit)
您应该更改为_myNSCalendarUnit = [boxed unsignedIntegerValue]
以确定符合类型。 long long
由64位定义,但NSUInteger不是(reference)。当创建盒装时,您应该使用相同的数据类型NSNumber *boxed = [NSNumber numberWithUnsignedInteger:self.myNSCalendarUnit];