[英]cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast
When converting an Objective-C program to a Objective-C ARC, I get the error:将 Objective-C 程序转换为 Objective-C ARC 时,出现错误:
"cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast "
The code is as follows:代码如下:
- (NSString *)_encodeString:(NSString *)string
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)string, // this is line in error
NULL,
(CFStringRef)@";/?:@&=$+{}<>,",
kCFStringEncodingUTF8);
return [result autorelease];
}
What is a bridged cast?什么是桥接演员表?
Have a look at the ARC documentation on the LLVM website.查看 LLVM 网站上的ARC 文档。 You'll have to use
__bridge
or one of the other keywords.您必须使用
__bridge
或其他关键字之一。
This is because Core Foundation objects (CF*Refs) are not controlled by ARC, only Obj-C objects are.这是因为 Core Foundation 对象 (CF*Refs) 不受 ARC 控制,只有 Obj-C 对象受控制。 So when you convert between them, you have to tell ARC about the object's ownership so it can properly clean them up.
所以当你在它们之间进行转换时,你必须告诉 ARC 对象的所有权,以便它可以正确地清理它们。 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).最简单的情况是
__bridge
cast,ARC 不会为此做任何额外的工作(它假设您自己处理对象的 memory)。
Here is a nice ARC tutorial that I found to be easier to understand than Apple's documentation that @jtbandes references.这是一个很好的 ARC 教程,我发现它比 @jtbandes 引用的 Apple 文档更容易理解。
Take a look at the section titled "Toll free bridging" in particular.请特别查看标题为“免费桥接”的部分。
I know this is an old thread, I came across this issue while I need to use我知道这是一个旧线程,我在需要使用时遇到了这个问题
- (NSString *)URLEncodedString
{
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL, CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
So what I did is go to Target > Build phase > Compile sources .所以我所做的是 go 到Target > Build phase > Compile sources 。 There is your file listed, double click on that and add following lines next to your file.
列出了您的文件,双击该文件并在文件旁边添加以下行。
-fno-objc-arc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.