2014-09-20 69 views
14

我知道这可能是重复的,但我警告我的ios项目更新的Xcode为版本6警告:隐式转换损失整数精度在Xcode 6

后首先得到了约30 隐式转换损失整数精度例如:

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; 

回答

20

NSArray countNSUInteger

NSIndexPath rowNSInteger

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

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

NSInteger index = indexPath.row/2; 

由于缺省警告,您可能会在Xcode 6中看到这些内容。您可以通过正确的警告设置轻松在Xcode 5中查看这些设置,并为64位构建。

32

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

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

Implicit Conversion to 32 Bit TypeNo

项目的构建设置。

enter image description here

+0

不应该是每个人都有的方式,但如果你知道该怎么做它的一个很好的提示! – 2015-03-03 17:32:17

+1

现在对于任何使用Unity的人来说都非常方便 – Tomskiis 2015-05-05 11:20:40

0

我总是通过这些警告恼火,所以我想出了简单的解决方案,以避免它:

@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 

只需从这一类,而不是NSIndexPath的行和部分使用rowIndexsectionIndex性质属性。