2014-09-20 71 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性質屬性。