警告:隐式转换在xcode 6中失去了整数精度

我知道这可能是重复的,但我得到了约30 隐隐转换失去整数精度警告在我的iOS项目更新到Xcode 6版本后。

第一个例子:

NSArray * stations = [self stationsJSON][KEY_ITEM_LIST]; int newSize = (stations.count + 1); // Implicit conversion loses Integer precision: 'unsigned long' to 'int' 

第二个例子:

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

我知道警告的意思。 使用NSInteger可以帮助避免此警告。

我不明白, 为什么在Xcode 5没有警告 ? 为什么我没有警告后,我改变了线路

 int index = indexPath.row / 2; 

 int index = indexPath.row / 2i; 

NSArray countNSUInteger

NSIndexPath rowNSInteger

在64位系统上, NSUIntegerNSInteger是64位,但int是32位。 所以这个值将不适合这个警告。

在iOS中最好避免int 。 相反,使用与您正在处理的值相同的types。

 NSInteger index = indexPath.row / 2; 

由于默认警告,您可能会在Xcode 6中看到这些内容。 你可以很容易地看到这些在Xcode 5中正确的警告设置和build设为64位。

您可以更新项目设置,删除所有

Implicit conversion loses integer precision警告,通过设置

Implicit Conversion to 32 Bit TypeNo

在项目的构build设置中。

在这里输入图像说明

我总是被这些警告所困扰,于是我想出了一个简单的解决scheme来避免它:

 @interface NSIndexPath(UnsignedIndex) @property (nonatomic, readonly) NSUInteger sectionIndex; @property (nonatomic, readonly) NSUInteger rowIndex; @end @implementation NSIndexPath(UnsignedIndex) - (NSUInteger)sectionIndex { return (NSUInteger)self.section; } - (NSUInteger)rowIndex { return (NSUInteger)self.row; } @end 

只需使用此类别中的rowIndexsectionIndex属性,而不是使用NSIndexPath的行和节属性。