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
NSIntegers를 위한 작업(%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
'programing' 카테고리의 다른 글
루비의 CSV로 배열 출력 (0) | 2023.06.01 |
---|---|
CSS 디스플레이 속성의 전환 (0) | 2023.06.01 |
터미널에서 swift를 사용하려면 어떻게 해야 합니까? (0) | 2023.05.27 |
깃허브에서 출발지와 상류의 차이점은 무엇입니까? (0) | 2023.05.27 |
Tslint - 유형이 사소한 추론 - 유형을 여기에 포함하는 것이 나쁜 관행인 이유는 무엇입니까? (0) | 2023.05.27 |