programing

NSInteger를 NSString 데이터 유형으로 변환하려면 어떻게 해야 합니까?

starjava 2023. 6. 1. 21:37
반응형

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)

%zdNSIntegers를 위한 작업(%tu(NSUInteger의 경우) 32비트 및 64비트 아키텍처 모두에서 캐스트 및 경고가 없습니다.저는 왜 이것이 "권장되는 방법"이 아닌지 모르겠습니다.

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

반응형