CFLocaleGetValue strange behaviour

Discussion in 'Mac Programming' started by THRESHE, Oct 2, 2008.

  1. THRESHE macrumors newbie

    Joined:
    Apr 10, 2008
    Location:
    Odessa, Ukraine
    #1
    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:
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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.
     
  3. THRESHE thread starter macrumors newbie

    Joined:
    Apr 10, 2008
    Location:
    Odessa, Ukraine
    #3
    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 ;)
     
  4. THRESHE thread starter macrumors newbie

    Joined:
    Apr 10, 2008
    Location:
    Odessa, Ukraine
    #4
    I've replaced CFLocaleGetSystem() by CFLocaleCopyCurrent() and now it works as desired though it's strange that CFLocaleGetSystem() fails...
     

Share This Page