Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

THRESHE

macrumors newbie
Original poster
Apr 10, 2008
6
0
Odessa, Ukraine
Hi
I want to use CFLocale in my application. But I've encountered a strange problem.

This code doesn't work
Code:
CFLocaleRef locale = CFLocaleGetSystem();
CFStringRef cfstr = CFStringCreateCopy(NULL, (CFStringRef)CFLocaleGetValue(locale, kCFLocaleGroupingSeparator)); 
qDebug()<<CFStringGetCStringPtr(cfstr, kCFStringEncodingMacRoman);
But this does
Code:
CFLocaleRef locale = CFLocaleGetSystem();
CFBooleanRef b = (CFBooleanRef) CFLocaleGetValue(locale, kCFLocaleUsesMetricSystem);
qDebug()<< CFBooleanGetValue(b);
Can't understand why :confused:
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Hi
I want to use CFLocale in my application. But I've encountered a strange problem.

This code doesn't work
Code:
CFLocaleRef locale = CFLocaleGetSystem();
CFStringRef cfstr = CFStringCreateCopy(NULL, (CFStringRef)CFLocaleGetValue(locale, kCFLocaleGroupingSeparator)); 
qDebug()<<CFStringGetCStringPtr(cfstr, kCFStringEncodingMacRoman);
But this does
Code:
CFLocaleRef locale = CFLocaleGetSystem();
CFBooleanRef b = (CFBooleanRef) CFLocaleGetValue(locale, kCFLocaleUsesMetricSystem);
qDebug()<< CFBooleanGetValue(b);
Can't understand why :confused:

1. You are using casts (CFStringRef) and (CFBooleanRef). Casts don't change what an object is, they only change what the compiler thinks the object is. This is very dangerous.

2. Read the documentation of CFStringGetCStringPtr. It is documented that the function can return NULL, and it is likely to do so if you ask for MacRoman text.

3. CFBooleanGetValue actually only checks whether the object you pass in is kCFBooleanTrue or not. So if you pass in kCFBooleanTrue, it returns true. If you pass in kCFBooleanFalse, it returns false. If you pass in anything else (a CFNumberRef, CFStringRef, or any garbage) it will always return FALSE.
 

THRESHE

macrumors newbie
Original poster
Apr 10, 2008
6
0
Odessa, Ukraine
When I debug cfstr variable value is "not a CFString" after the value is returned plus CFShow prints "(null)".

P.S. I know about casting types ;)
 

THRESHE

macrumors newbie
Original poster
Apr 10, 2008
6
0
Odessa, Ukraine
I've replaced CFLocaleGetSystem() by CFLocaleCopyCurrent() and now it works as desired though it's strange that CFLocaleGetSystem() fails...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.