0

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?

S.J
  • 3,063
  • 3
  • 33
  • 66

1 Answers1

0

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];

Community
  • 1
  • 1
nynohu
  • 1,628
  • 12
  • 12
  • but I used numberWithUnsignedLong to convert into NSNumber... will converting to unsignedIntegerValue can impact in ay way? – S.J Nov 29 '16 at 09:07
  • 1
    The most appropriate way is you create [NSNumber numberWithUnsignedInteger:self.myNSCalendarUnit];` with original type of NSCalendar (NSUInteger). It should not lost data of any case. Further, in your current code, you create with `unsignedLong` but get with `unsignedLongLong`, it is not approciate. – nynohu Nov 29 '16 at 09:19