Jak konwertować NSIntegerna NSStringtyp danych?
Wypróbowałem następujące, gdzie miesiąc to NSInteger:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
Jak konwertować NSIntegerna NSStringtyp danych?
Wypróbowałem następujące, gdzie miesiąc to NSInteger:
NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];
Odpowiedzi:
NSIntegers nie są obiektami, na które rzucasz je long, aby dopasować definicję obecnej architektury 64-bitowej:
NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];
[@(integerValue) stringValue]to czystsze podejście.
Sposób Obj-C =):
NSString *inStr = [@(month) stringValue];
An NSIntegerma metodę, stringValuektórej można używać nawet z literałem
NSString *integerAsString1 = [@12 stringValue];
NSInteger number = 13;
NSString *integerAsString2 = [@(number) stringValue];
Bardzo prosty. Prawda?
var integerAsString = String(integer)
%zddziała dla NSIntegers ( %tudla NSUInteger) bez rzutowań i bez ostrzeżeń na architekturach 32-bitowych i 64-bitowych. Nie mam pojęcia, dlaczego nie jest to „ zalecany sposób ”.
NSString *string = [NSString stringWithFormat:@"%zd", month];
Jeśli interesuje Cię, dlaczego to działa, zobacz to pytanie .
Łatwy sposób:
NSInteger value = x;
NSString *string = [@(value) stringValue];
Tutaj @(value)konwertuje dane NSIntegerdo NSNumberobiektu, dla którego można wywołać żądaną funkcję stringValue.
Podczas kompilacji z obsługą dla arm64, nie spowoduje to wygenerowania ostrzeżenia:
[NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
Możesz też spróbować:
NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: @"%ld", month];
Format specifies type 'int' but the argument has type 'NSInteger *'(aka 'int *'). Zamiast według docs firmy Apple , poszedłem zNSString *inStr = [NSString stringWithFormat:@"%d", (int)month];