문제

I have the following code in my Application:

static void foo(CFStringRef str)
{
    CFStringEncoding encoding = CFStringGetSystemEncoding();
    const char * cString = CFStringGetCStringPtr(str, encoding);

    //.....                
}

It's been around since iOS 5, and always worked. Since iOS 7 release, CFStringGetCStringPtr returns NULL. Adding the following code, have solved it:

if (cString==NULL)
{
    cString = [
        ((NSString *)str) cStringUsingEncoding:[NSString defaultCStringEncoding]
    ];
}

Still, I would like to know if anyone knows what causes the problem?

도움이 되었습니까?

해결책

CFStringGetCStringPtr() isn't guaranteed to return non-NULL. From the docs (emphasis added):

Whether or not this function returns a valid pointer or NULL depends on many factors, all of which depend on how the string was created and its properties. In addition, the function result might change between different releases and on different platforms. So do not count on receiving a non-NULL result from this function under any circumstances.

Always have a fallback to CFStringGetCString(), but even better, use Objective-C and NSString's helper methods (e.g. UTF8String).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top