Warning: Implicit conversion loses Integer precision in xcode 6

后端 未结 3 614
逝去的感伤
逝去的感伤 2020-12-29 06:36

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

相关标签:
3条回答
  • 2020-12-29 07:12

    NSArray count is NSUInteger.

    NSIndexPath row is NSInteger.

    On 64-bit systems, NSUInteger and NSInteger are 64-bits but int is 32-bit. So the value won't fit which results in the warning.

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

    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.

    0 讨论(0)
  • 2020-12-29 07:25

    You can update project settings, to remove all

    Implicit conversion loses integer precision warnings, by setting

    Implicit Conversion to 32 Bit Type to No

    in project's build settings.

    enter image description here

    0 讨论(0)
  • 2020-12-29 07:35

    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 rowIndex and sectionIndex properties from this category instead of NSIndexPath's row and section properties.

    0 讨论(0)
提交回复
热议问题