0
votes

I am using this code to encode and decode 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;
}

But I am getting this warning

Implicit conversion loses integer precision: 'unsigned long long' to 'NSCalendarUnit' (aka 'enum NSCalendarUnit')

How to correct code to remove this warning?

1

1 Answers

0
votes

As you can see the description type of NSCalendarUnit

typedef NS_OPTIONS(NSUInteger, NSCalendarUnit)

You should change to _myNSCalendarUnit = [boxed unsignedIntegerValue] for conform type. The long long is defined by 64-bit, but NSUInteger is not (reference). And when create boxed, you should use with the same datatype NSNumber *boxed = [NSNumber numberWithUnsignedInteger:self.myNSCalendarUnit];