【问题标题】:Warning: Implicit conversion loses Integer precision in xcode 6警告:隐式转换在 xcode 6 中会丢失整数精度
【发布时间】:2014-09-20 23:16:10
【问题描述】:

我知道它可能是重复的,但是在将 xcode 更新到版本 6 后,我在我的 ios 项目中收到了大约 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;

【问题讨论】:

    标签: ios objective-c xcode warnings


    【解决方案1】:

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

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

    Implicit Conversion to 32 Bit TypeNo

    在项目的构建设置中。

    【讨论】:

    • 不应该是每个人都应该走的路,但如果你知道该怎么做,这是一个很好的提示!
    • 对于目前使用 Unity 的任何人来说真的很方便
    【解决方案2】:

    NSArray countNSUInteger

    NSIndexPath rowNSInteger

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

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

    NSInteger index = indexPath.row / 2;
    

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

    【讨论】:

      【解决方案3】:

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

      @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 的行和节属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-30
        相关资源
        最近更新 更多