Benches Uncategorized

10

NSNumberFormatter and 2.1 SDK ?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello all,

Anyone knows if the 2.1 SDK/IPhone update fixed the NSNumberformatter problem?

(i.e : that it worked in the simulator, but not on the device)

thanks

10 replies

Sign in or join Beachball Help to reply.

  1. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ah, nevermind..my fault. I actually used numberRef two times and both times with CFNumberCreate(), so apparently I instantiated two NSCFNumbers and only released one.

    So, this appears to be a good workaround after all.

  2. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    No it isn't. Such a shame!

  3. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Now, since I still needed that functionality on the device as well, I looked through the C references and set up a number formatter this way. Works like a charm, just not exactly as nicely wrapped up like the Objective-C variant.


    CFNumberFormatterRef formatterRef = CFNumberFormatterCreate(NULL, CFLocaleCopyCurrent(), kCFNumberFormatterNoStyle);
    CFStringRef format = CFStringCreateWithCString(NULL, "###,###", kCFStringEncodingUTF8);
    CFNumberFormatterSetFormat(formatterRef, format);
    CFNumberRef numberRef = CFNumberCreate(NULL, 9, &someIntegerValue);
    yourFromattedNSString = (NSString *)CFNumberFormatterCreateStringWithNumber(NULL, formatterRef, numberRef);

    Hope that helps, until they get it fixed. It's fully locale dependent, so people in different countries will see "1.000", "1 000", "1,000" or whatever their locale says. Read up on the docs about those CFNumber... and CFString... stuff, if you want to understand it better or change it to what you need.

    Message was edited by: leberwurstsaft

  4. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Now I'm having a hard time eliminating the occuring memory leaks imposed by this.

    Somehow there are some NSCFNumber and NSCFString leaks. I'm using CFRelease() formatter, format, numberRef and also formattedNumberRef (which I created additionally, to accomodate the fact, that CFNumberFormatterCreateStringWithNumber gives me the ownership of that string).

    So, I more or less have something like this:


    CFNumberFormatterRef formatterRef = CFNumberFormatterCreate(NULL, CFLocaleGetSystem(), kCFNumberFormatterNoStyle);
    CFStringRef format = CFStringCreateWithCString(NULL, "###,###", kCFStringEncodingUTF8);
    CFNumberFormatterSetFormat(formatterRef, format);
    CFNumberRef numberRef = CFNumberCreate(NULL, 9, &someIntegerValue);
    CFStringRef formattedNumberRef = CFNumberFormatterCreateStringWithNumber(NULL, formatterRef, numberRef);
    yourFormattedNSString = (NSString *)formattedNumberRef;
    CFRelease(formattedNumberRef); CFRelease(numberRef); CFRelease(format); CFRelease(formatterRef);

    But I can read in Instruments, that there is a NSCFNumber being allocated (malloc), but not released.

    Or sometimes being copied (by copyWithZone) and released, but still not freed.

    Usually it should go: malloc, blah blah, release, free. And it does for the formatter etc, but somehow my CFRelease() calls are not enough?!

    Message was edited by: leberwurstsaft

    (forgot releases)

    (replaced CFLocaleCopyCurrent() by CFLocaleGetSystem(), which gets the same locale, but isn't a copy, just a reference, which I wouldn't have to release)

  5. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ah, nevermind..my fault. I actually used numberRef two times and both times with CFNumberCreate(), so apparently I instantiated two NSCFNumbers and only released one.

    So, this appears to be a good workaround after all.

  6. 0

    aapl.crox

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    can this kind of foramting be used to format phone numbers according to locales?

  7. 0

    Ostrowsky S

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thank you !

    for the reply and the details !

    I ended up writing my own little formatter myself, easily done since the numbers I deal with are always under 9 digits...

    But I am waiting for a fix !

    regards

  8. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    no, I couldn't find anything about phone number formatting in there. just currencies, dates and other numbers. this code above is set to no premade style, so you can just setup any format you want, just read up about the syntax of the format string.

    additionally I wanted to point out that CFLocaleGetSystem() wasn't correct, as it returned just the English locale and not the one that is actually in use.

    I'm now using this:


    CFLocaleRef localeRef = (CFLocaleRef)[NSLocale currentLocale];

    (and replaced CFLocaleGetSystem() with localeRef, no need to CFRelease() it, as well)

  9. 0

    aapl.crox

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks but doing the formating for all phone numbers in the world would be too big of a task. clearly apple already has this coded, there must be a way to reuse it?

  10. 0

    leberwurstsaft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I suppose they've done it, but it might be in one of their private frameworks. At least I couldn't find anything like it in the docs.