简体   繁体   English

Objective-C 指针类型“NSString *”到 C 指针类型“CFStringRef”(又名“const struct __CFString *”)的转换需要桥接转换

[英]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.

相关问题 不兼容的指针类型将“const CFStringRef”(又名“const struct __CFString *const”)发送到“id”类型的参数 - Incompatible pointer types sending 'const CFStringRef' (aka 'const struct __CFString *const') to parameter of type 'id' ARC禁止将非目标C指针类型&#39;const UInt8 *&#39;(aka&#39;const unsigned char *&#39;)强制转换为&#39;NSData *&#39; - Cast of a non-Objective-C pointer type 'const UInt8 *' (aka 'const unsigned char *') to 'NSData *' is disallowed with ARC 将__bridge之后的object-c指针类型nsoutputstream *强制转换为c指针类型cfwritestreamref异常 - cast of objective-c pointer type nsoutputstream* to c pointer type cfwritestreamref exception after __bridge 指向Objective-C指针的间接指针的强制转换 - cast of indirect pointer to an Objective-C pointer 为什么将类型强制转换为id会摆脱此错误消息“类型为&#39;CGColorRef&#39;的集合元素(又名&#39;struct CGColor *&#39;)不是Objective-C对象” - Why type cast to id will get rid of this error message “Collection element of type 'CGColorRef' (aka 'struct CGColor *') is not an Objective-C object” 如何在Objective-C中转换间接指针 - how to cast an indirect pointer in Objective-C ObjC:对Objective-C指针的间接指针的强制转换 - ObjC: cast of an indirect pointer to an Objective-C pointer 使用ARC不允许使用指向'CFReadStreamRef *'的Objective-C指针的间接指针 - Cast of an indirect pointer to an Objective-C pointer to 'CFReadStreamRef *' is disallowed with ARC 在Objective-C中强制转换为struct - Cast to struct in Objective-C Objective-C类作为C指针类型 - Objective-C class as C pointer type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM