ios7 - CFStringGetCStringPtr 在 iOS7 上返回 NULL

标签 ios7 cfstring

我的应用程序中有以下代码:

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

    //.....                
}

它自 iOS 5 以来就已存在,并且始终有效。 自 iOS 7 发布以来,CFStringGetCStringPtr 返回 NULL。 添加以下代码,已解决:

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

不过,我想知道是否有人知道导致问题的原因?

最佳答案

CFStringGetCStringPtr() 不保证返回非 NULL。来自文档(添加了重点):

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.

始终使用 CFStringGetCString(),但更好的是,使用 Objective-C 和 NSString 的辅助方法(例如 UTF8String)。

关于ios7 - CFStringGetCStringPtr 在 iOS7 上返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18976485/

相关文章:

ios - UniChar(来自 CFString)和 unichar(来自 NSString)是一样的吗?

cocoa - CFData 与 CFString

swift - 如何在 OS X 目标中创建 CFDictionary?

ios - 以编程方式/自动创建 ViewController

ios - 应用程序错误 <错误> : CGContextRestoreGState: invalid context 0x0

ios7 - 使用 AFHttpRequestOperationManager 发布请求不起作用

iphone - 释放 CFString

cocoa - 将 CFStringRef 指针传递给 MacRuby 中的 C 函数

ios - UIActivityViewController => LaunchServices : invalidationHandler called

ios - 使用 Touch ID 将指纹存储在数据库中