Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: Implicit conversion loses Integer precision in xcode 6

I know it could be a duplicate, but i got about 30 Implicit conversion loses Integer precision warnings in my ios project after updating xcode to version 6.

First Example:

NSArray * stations = [self stationsJSON][KEY_ITEM_LIST];

int newSize = (stations.count + 1); // Implicit conversion loses Integer precision: 'unsigned long' to 'int'

Second Example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    int index = indexPath.row / 2; // Implicit conversion loses Integer precision: 'long' to 'int'
    ...
}

I know what the warning means. Using NSInteger can help to avoid this warning.

I don't understand, why there was no warnings in xcode 5? And why there is no warning after i change the line

int index = indexPath.row / 2;

to

int index = indexPath.row / 2i;
like image 984
dieter Avatar asked Sep 20 '14 23:09

dieter


2 Answers

You can update project settings, to remove all

Implicit conversion loses integer precision warnings, by setting

Implicit Conversion to 32 Bit Type to No

in project's build settings.

enter image description here

like image 91
Vitalii Gozhenko Avatar answered Oct 22 '22 18:10

Vitalii Gozhenko


NSArray count is NSUInteger.

NSIndexPath row is NSInteger.

On 64-bit systems, NSUInteger and NSInteger are 64-bits but int is 32-bit. So the value won't fit which results in the warning.

It's best to avoid int in iOS. Instead, use the same type as the values you are dealing with.

NSInteger index = indexPath.row / 2;

You probably see these in Xcode 6 due to the default warnings. You can easily see these in Xcode 5 with the right warning settings and building for 64-bit.

like image 31
rmaddy Avatar answered Oct 22 '22 18:10

rmaddy