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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25954180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:40:08  来源:igfitidea点击:

Warning: Implicit conversion loses Integer precision in xcode 6

iosobjective-cxcodewarnings

提问by dieter

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

我知道它可能是重复的,但是在将 xcode 更新到版本 6 后,我的 ios 项目中出现了大约 30 个隐式转换丢失整数精度警告。

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.

我知道警告是什么意思。使用 NSInteger 可以帮助避免这个警告。

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

我不明白,为什么 xcode 5 中没有警告?为什么我换行后没有警告

int index = indexPath.row / 2;

to

int index = indexPath.row / 2i;

回答by Vitaliy Gozhenko

You can update project settings, to remove all

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

Implicit conversion loses integer precisionwarnings, by setting

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

Implicit Conversion to 32 Bit Typeto No

Implicit Conversion to 32 Bit TypeNo

in project's build settings.

在项目的构建设置中。

enter image description here

在此处输入图片说明

回答by rmaddy

NSArray countis NSUInteger.

NSArray countNSUInteger

NSIndexPath rowis NSInteger.

NSIndexPath rowNSInteger

On 64-bit systems, NSUIntegerand NSIntegerare 64-bits but intis 32-bit. So the value won't fit which results in the warning.

在 64 位系统上,NSUInteger并且NSInteger是 64 位但是int是 32 位。因此该值不适合导致警告。

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

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

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.

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

回答by Matthes

I was always annoyed by these warnings, so I came up with simple solution to avoid it:

我总是对这些警告感到恼火,所以我想出了一个简单的解决方案来避免它:

@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

Simply use rowIndexand sectionIndexproperties from this category instead of NSIndexPath's row and section properties.

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