创建一个不可变对象CFArray - 苹果样品不工作 [英] Creating an immutable CFArray object - Apple sample doesn't work
问题描述
有关测试我复制下面的示例code [<一个href=\"https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFCollections/Articles/creating.html]\" rel=\"nofollow\">https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFCollections/Articles/creating.html]并编译它:
For testing I copied following sample code [https://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFCollections/Articles/creating.html] and compiled it:
#import <CoreFoundation/CoreFoundation.h>
CFStringRef strs[3];
CFArrayRef anArray;
strs[0] = CFSTR("String One");
strs[1] = CFSTR("String Two");
strs[2] = CFSTR("String Three");
anArray = CFArrayCreate(NULL, (void *)strs, 3, &kCFTypeArrayCallBacks);
现在我得到了以下错误:
Now I got following error: "No matching function for call to CFArrayCreate"
为什么不编译,以及如何实现它,这是编译?
Why it is not compilable and how to implement it that it is compilable?
推荐答案
类型 CFArrayCreate()
是常量无效的第二个参数的* *
。因此,呼叫更改为:
The type of the second parameter of CFArrayCreate()
is const void **
. So, change the call to:
anArray = CFArrayCreate(NULL, (const void **)strs, 3, &kCFTypeArrayCallBacks);
这是真的只有在C ++中的一个问题,因为它是关于转换和从无效*
更严格了很多。在C,无效*
转换为其他自由指针类型。
This is really only a problem in C++, because it is a lot stricter about converting to and from void*
. In C, void*
converts to other pointer types freely.
这篇关于创建一个不可变对象CFArray - 苹果样品不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!