如何修复隐式转换会丢失整数精度警告

时间:2016-11-29 07:18:03

标签: ios objective-c nsnumber nscalendar

我正在使用此代码对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')

如何更正代码以删除此警告?

1 个答案:

答案 0 :(得分:0)

正如您可以看到NSCalendarUnit的描述类型

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit)

您应该更改为_myNSCalendarUnit = [boxed unsignedIntegerValue]以确定符合类型。 long long由64位定义,但NSUInteger不是(reference)。当创建盒装时,您应该使用相同的数据类型NSNumber *boxed = [NSNumber numberWithUnsignedInteger:self.myNSCalendarUnit];