gpt4 book ai didi

objective-c - 如何正确使用 Foundation 的 NSString 和 CoreFoundation 函数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:51:32 25 4
gpt4 key购买 nike

我想简化我的一些钥匙串(keychain)服务代码,并使用 CFDictionarySetValue 和 Foundation 的 NSString

CFDictionarySetValue 的声明如下:

void CFDictionarySetValue(CFMutableDictionaryRef theDict, const void *key, const void *value)

所以当我通过时会发生什么@"This is a NSString" for value 参数?在我的例子中,编译器不报告警告,静态分析也没有捕获任何东西。在运行时,没有崩溃,这是否意味着运行时会处理所有事情,或者我应该传递 [@"something"cStringUsingEncoding:NSUTF8StringEncoding] 并将其转换为 const void*?

我的发现表明:

NSLog(@"%s", CFDictionaryGetValue(query, kKeyForCStringInUTF8));
NSLog(@"%@", CFDictionaryGetValue(query, kKeyForNSString));

两者都给出相同的输出!令人困惑...

在 CF 和 Foundation 之间交换对象的一般规则是什么?是否有普遍接受的代码风格,一种好的做法?

最佳答案

NSString 和其他类型可以免费桥接到它们的 CoreFoundation 对应类型。
参见 Core Foundation Design Concepts - Toll-Free Bridged Types :

There are a number of data types in the Core Foundation framework and the Foundation framework that can be used interchangeably. This means that you can use the same data structure as the argument to a Core Foundation function call or as the receiver of an Objective-C message invocation.

关于objective-c - 如何正确使用 Foundation 的 NSString 和 CoreFoundation 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8887255/

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