2017-03-06 13 views
1

CFNumberFormatterCreateStringWithNumber과 관련된 문제가 있습니다.CFNumberFormatterCreateStringWithNumber int issue

// Works fine with float: 
float n = 1.20; 
CFNumberRef value = CFNumberCreate(NULL, kCFNumberFloatType, &n); 
CFLocaleRef currentLocale = CFLocaleCopyCurrent(); 
// Create different number formatters 
CFNumberFormatterRef decimalFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterDecimalStyle); 
CFStringRef decimalString = CFNumberFormatterCreateStringWithNumber(NULL, decimalFormatter, value); 

// Using an INT I see an issue 
int n = 892385; 
CFNumberRef value = CFNumberCreate(NULL, kCFNumberSInt32Type, &n); 
CFLocaleRef currentLocale = CFLocaleCopyCurrent(); 
// Create different number formatters 
CFNumberFormatterRef decimalFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterDecimalStyle); 
CFStringRef decimalString = CFNumberFormatterCreateStringWithNumber(NULL, decimalFormatter, value); 
/// release, release, release 

(대신 부동의) int 값을 사용할 때 표시되는 오류는 다음과 같습니다 : 거기

<CFNumber 0x5ac27 [0x7fff9856ed80]>{value = +1452, type = kCFNumberSInt32Type} 
2017-03-05 21:42:06.789577 HID_Dumper[4390:150864] -[__NSCFNumber _getCString:length:multiplier:]: unrecognized selector sent to instance 0x2e4027 

답변

2

귀하의 코드는 경험적으로 나를 위해 일한 나는 this Apple code example을 사용했다.

#import <CoreFoundation/CoreFoundation.h> 

int main(int argc, const char * argv[]) { 
    @autoreleasepool { 
     // insert code here... 
     NSLog(@"Hello, World!"); 

     // Works fine with float: 
     float n = 1.20; 
     CFNumberRef value = CFNumberCreate(NULL, kCFNumberFloatType, &n); 
     CFLocaleRef currentLocale = CFLocaleCopyCurrent(); 
     // Create different number formatters 
     CFNumberFormatterRef decimalFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterDecimalStyle); 
     CFStringRef decimalString = CFNumberFormatterCreateStringWithNumber(NULL, decimalFormatter, value); 
     NSLog(@"float is %@", (__bridge NSString *)decimalString); 
     CFRelease(decimalFormatter); 
     CFRelease(decimalString); 

     // Using an INT I see an issue 
     SInt32 duh = 892385; 
     value = CFNumberCreate(NULL, kCFNumberSInt32Type, &duh); 
     currentLocale = CFLocaleCopyCurrent(); 
     // this is the exact same formatter as up there... 
     decimalFormatter = CFNumberFormatterCreate(NULL, currentLocale, kCFNumberFormatterDecimalStyle); 
     decimalString = CFNumberFormatterCreateStringWithNumber(NULL, decimalFormatter, value); 

     NSLog(@"integer is %@", (__bridge NSString *)decimalString); 
     /// release, release, release 
     CFRelease(decimalFormatter); 
     CFRelease(decimalString); 
    } 
    return 0; 
} 

의 출력 결과 : 난 단지 화장품 변화의 몇 가지를 만드는 데 필요한

2017-03-05 22:31:49.502 Testing[3278:221264] Hello, World! 
2017-03-05 22:31:49.510 Testing[3278:221264] float is 1.2 
2017-03-05 22:31:49.511 Testing[3278:221264] integer is 892,385 
Program ended with exit code: 0 
0

그것은 내가 컴파일하고 코드를 C++에서 실행 되었기 때문에 문제가 해결되지 않았다 밝혀 (.cpp 확장자), 파일 확장자를 .mm (Objective-C++)으로 바꿀 때 작동합니다.