Implicit conversion loses integer precision

Hi,


I am using the code below to call data using PHP from my web site, her is my NSObject code .h:


-(instancetype)initWithId:(int)Id Name:(NSString *)currentName City:(NSString

*)givenCity Address:(NSString *)givenAddress;

@property (nonatomic) int Id;

@property (nonatomic,strong) NSString * city;

@property (nonatomic,strong) NSString * name;

@property (nonatomic,strong) NSString *address;



Her is my NSObject code .m:


-(instancetype)initWithId:(int)Id Name:(NSString *)currentName City:(NSString

*)givenCity Address:(NSString *)givenAddress{

self = [super init];

if(self){

self.Id = Id;

self.name = currentName;

self.city = givenCity;

self.address = givenAddress;

}

return self;

}



Her is where I call the data:


NSURL *blogURL = [NSURL URLWithString:JSON_URL];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization

JSONObjectWithData:jsonData options:0 error:&error];

for (NSDictionary *bpDictionary in dataDictionary) {

HotelObject *currenHotel = [[HotelObject alloc]initWithId:[[bpDictionary

objectForKey:@"timeLineVideoUserName"]integerValue] Name:[bpDictionary objectForKey:@"timeLineVideoUserName"]



// in the line above the Xcode shows this message:

Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int.



City:[bpDictionary objectForKey:@"timeLineVideoUserName"] Address:[bpDictionary

objectForKey:@"timeLineVideoDetails"]];

[self.objectHolderArray addObject:currenHotel];


}


How can I solve this problem?

In HotelObject's initializer method, replace the "int" declaration of your "Id" parameter with NSInteger (or NSUInteger if it doesn't need to be signed).

or change

initWithId:[[bpDictionary objectForKey:@"timeLineVideoUserName"]integerValue]

to

initWithId:[[bpDictionary objectForKey:@"timeLineVideoUserName"]intValue]

I would recommend that you stick to NSInteger / NSUinteger for integer values and CGFloat for floating point values (float and double). These are the default types used in the Apple libraries. They will also use the largest size (32 or 64 bit for integers) supported by the target CPU.


Integer values like short, int, long, long long etc are simply defined as having a size relationship like this:


short ≤ int ≤ long ≤ long long


with no guarantee for the actual bit size. Type sizes can vary between compilers / OS platform.


There are also a number of fixed size types (int8_t, int16_t, ... uint8_t, uint16_t, ...) if you need to ensure a specific size.

Implicit conversion loses integer precision
 
 
Q