gpt4 book ai didi

ios7 - CFStringGetCStringPtr 在 iOS7 上返回 NULL

转载 作者:行者123 更新时间:2023-12-02 08:11:13 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com