programing tip

NSInteger를 NSString 데이터 유형으로 어떻게 변환합니까?

itbloger 2020. 6. 30. 20:54
반응형

NSInteger를 NSString 데이터 유형으로 어떻게 변환합니까?


어떻게 변환합니까 NSInteger받는 NSString데이터 유형을?

나는 달이 다음과 같은 것을 시도했다 NSInteger:

  NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];

NSInteger는 객체가 아니며 long현재 64 비트 아키텍처의 정의와 일치시키기 위해 캐스트합니다 .

NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];


Obj-C 방법 =) :

NSString *inStr = [@(month) stringValue];

현대 목표 -C

NSInteger방법 갖는 stringValue리터로에도 사용될 수있다

NSString *integerAsString1 = [@12 stringValue];

NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];

매우 간단합니다. 그렇지 않습니까?

빠른

var integerAsString = String(integer)

%zd%tu32 비트 및 64 비트 아키텍처 모두에서 캐스트 및 경고가없는 NSInteger ( NSUInteger의 경우)에서 작동합니다 . 이것이 " 권장 방법 " 이 아닌 이유를 모르겠습니다 .

NSString *string = [NSString stringWithFormat:@"%zd", month];

왜 이것이 효과가 있는지에 관심 이 있다면이 질문을 참조하십시오 .


쉬운 방법 :

NSInteger value = x;
NSString *string = [@(value) stringValue];

여기에서 @(value)주어진 함수를 필요한 함수를 호출 할 수 NSInteger있는 NSNumber객체 로 변환합니다 stringValue.


에 대한 지원으로 컴파일 할 때 arm64경고가 생성되지 않습니다.

[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];

시도해 볼 수도 있습니다 :

NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];

답이 주어 지지만 어떤 상황에서는 이것이 NSInteger에서 문자열을 얻는 흥미로운 방법이 될 것이라고 생각합니다.

NSInteger value = 12;
NSString * string = [NSString stringWithFormat:@"%0.0f", (float)value];

이 경우 NSNumber가 적합 할 수 있습니다.

NSString *inStr = [NSString stringWithFormat:@"%d", 
                    [NSNumber numberWithInteger:[month intValue]]];

참고 URL : https://stackoverflow.com/questions/1796390/how-do-i-convert-nsinteger-to-nsstring-datatype

반응형