Objective-C指针类型'NSString *'转换为C指针类型'CFStringRef'(aka'const struct __CFString *')需要一个桥接 [英] cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast
问题描述
将Objective-C程序转换为Objective-C ARC时,会出现以下错误:
When converting an Objective-C program to a Objective-C ARC, I get the error:
"cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast "
代码如下:
- (NSString *)_encodeString:(NSString *)string
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)string, // this is line in error
NULL,
(CFStringRef)@";/?:@&=$+{}<>,",
kCFStringEncodingUTF8);
return [result autorelease];
}
什么是桥梁施法?
推荐答案
查看 ARC文档 LLVM网站。您必须使用 __ bridge
或其他关键字之一。
Have a look at the ARC documentation on the LLVM website. You'll have to use __bridge
or one of the other keywords.
这是因为Core Foundation对象* Refs)不受ARC控制,只有Obj-C对象。所以当你在它们之间转换,你必须告诉ARC关于对象的所有权,以便它可以正确清理。最简单的情况是 __ bridge
cast,ARC不会做任何额外的工作(它假设你自己处理对象的内存)。
This is because Core Foundation objects (CF*Refs) are not controlled by ARC, only Obj-C objects are. So when you convert between them, you have to tell ARC about the object's ownership so it can properly clean them up. The simplest case is a __bridge
cast, for which ARC will not do any extra work (it assumes you handle the object's memory yourself).
这篇关于Objective-C指针类型'NSString *'转换为C指针类型'CFStringRef'(aka'const struct __CFString *')需要一个桥接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!