[objective-c] Objective-C 암시 적 변환은 정수 정밀도 ‘NSUInteger'(일명 ‘부호없는 long’)를 ‘int’경고로 잃습니다.

몇 가지 연습을 진행 중이며 다음과 같은 경고가 표시됩니다.

암시 적 변환은 정수 정밀도를 잃습니다 : ‘NSUInteger'(일명 ‘unsigned long’)에서 ‘int’

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSArray *myColors;

        int i;
        int count;

        myColors = @[@"Red", @"Green", @"Blue", @"Yellow"];

        count = myColors.count; //  <<< issue warning here

        for (i = 0; i < count; i++)

        NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
    }

    return 0;
}

스크린 샷



답변

count방법 NSArray다시 표시 NSUInteger하고, 64 비트 OS X 플랫폼

  • NSUInteger로 정의 unsigned long되고
  • unsigned long 부호없는 64 비트 정수입니다.
  • int 32 비트 정수입니다.

따라서 int“보다 작은”데이터 유형 NSUInteger이므로 컴파일러 경고입니다.

“Foundation Data Types Reference”의 NSUInteger 도 참조하십시오 .

32 비트 응용 프로그램을 빌드 할 때 NSUInteger는 부호없는 32 비트 정수입니다. 64 비트 응용 프로그램은 NSUInteger를 부호없는 64 비트 정수로 취급합니다.

해당 컴파일러 경고를 수정하려면 로컬 count변수를 다음과 같이 선언하십시오.

NSUInteger count;

또는 (배열에 2^31-1요소를 초과하지 않을 것이라고 확신하는 경우 ) 명시 적 캐스트를 추가하십시오.

int count = (int)[myColors count];


답변

Martin의 대답과 달리 int로 캐스팅하거나 경고를 무시하는 것이 배열에 2 ^ 31-1 개 이상의 요소가 없다는 것을 알고 있어도 항상 안전하지는 않습니다. 64 비트로 컴파일 할 때는 아닙니다.

예를 들면 다음과 같습니다.

NSArray *array = @[@"a", @"b", @"c"];

int i = (int) [array indexOfObject:@"d"];
// indexOfObject returned NSNotFound, which is NSIntegerMax, which is LONG_MAX in 64 bit.
// We cast this to int and got -1.
// But -1 != NSNotFound. Trouble ahead!

if (i == NSNotFound) {
    // thought we'd get here, but we don't
    NSLog(@"it's not here");
}
else {
    // this is what actually happens
    NSLog(@"it's here: %d", i);

    // **** crash horribly ****
    NSLog(@"the object is %@", array[i]);
}


답변

프로젝트> 빌드 설정에서 키 변경 ” printf / scanf에 대한 typecheck 호출 : NO

설명 : [작동 원리]

제공된 인수에 지정된 형식 문자열에 적합한 유형이 있고 형식 문자열에 지정된 변환이 의미가 있는지 printf 및 scanf 등의 호출을 확인하십시오.

그것이 작동하기를 바랍니다

다른 경고

목적 c 내재적 변환은 정수 정밀도 ‘NSUInteger'(일명 ‘unsigned long’)를 ‘int’로 잃습니다.

키 ” 암시 적 변환을 32 비트로 변경 > 유형> 디버그> * 64 아키텍처 : 아니오

[ 주의 : 64 비트 아키텍처 변환에 대한 다른 경고가 무효화 될 수 있습니다] .


답변

expicit을 “int”로 캐스팅하면 내 문제가 해결됩니다. 나는 같은 문제가 있었다. 그래서:

int count = (int)[myColors count];


답변