简体   繁体   English

使用ARC不允许使用指向'CFReadStreamRef *'的Objective-C指针的间接指针

[英]Cast of an indirect pointer to an Objective-C pointer to 'CFReadStreamRef *' is disallowed with ARC

I want to use CFStreamCreatePairWithSocketToHost with NSInput - and OutputStream s. 我想将CFStreamCreatePairWithSocketToHostNSInputOutputStream一起使用。 I have two ivars NSInputStream *_inputStream and NSOutputStream *_outputStream . 我有两个ivars NSInputStream *_inputStreamNSOutputStream *_outputStream

The following gives me two error messages: 以下给出了两条错误消息:

CFStreamCreatePairWithSocketToHost(NULL,
  (__bridge_retained CFStringRef)self.hostname, self.port,
  (CFReadStreamRef *)&_inputStream, (CFWriteStreamRef *)&_outputStream);

error: cast of an indirect pointer to an Objective-C pointer to ' CFReadStreamRef * ' (aka ' struct __CFReadStream ** ') is disallowed with ARC 错误:ARC不允许使用指向' CFReadStreamRef * '(又名' struct __CFReadStream ** ')的Objective-C指针的间接指针
error: cast of an indirect pointer to an Objective-C pointer to ' CFWriteStreamRef * ' (aka ' struct __CFWriteStream ** ') is disallowed with ARC 错误:ARC不允许使用指向' CFWriteStreamRef * '(又名' struct __CFWriteStream ** ')的Objective-C指针的间接指针

How would I fix this? 我该如何解决这个问题? I tried using __bridge but I got similar error messages. 我尝试使用__bridge但我得到了类似的错误消息。

Pass a pointer to actual CFReadStreamRef and CFWriteStreamRef variables, then cast when assigning to your NSTypes. 传递指向实际CFReadStreamRefCFWriteStreamRef变量的指针,然后在分配给NSType时进行转换。 Basically: 基本上:

CFThingRef cfThing = NULL;
CFFunctionGetThing(&cfThing);
NSThing * nsThing = cfThing;

Of course, you'll have to add the appropriate bridge cast and do all appropriate reference count ops for cfThing . 当然,您必须添加适当的桥接转换并为cfThing执行所有适当的引用计数操作。

Think about what ARC is doing, and what __bridge casts do: ARC takes responsibility for retaining / releasing NSObjects. 想想ARC正在做什么,以及__bridge演员做什么:ARC负责保留/释放NSObject。 __bridge casts transfer that responsibility. __bridge转移了这项责任。 For example, __bridge_retained retains an NSString, cast the pointer to CFStringRef, and handed the responsibility for doing the matching release operation to whoever uses the CFStringRef. 例如,__ bridge_retained保留NSString,将指针强制转换为CFStringRef,并将负责执行匹配的释放操作交给使用CFStringRef的人。

That cannot possibly work with a pointer to an NSObject* or a pointer to a Core Foundation ref. 不可能使用指向NSObject *的指针或指向Core Foundation ref的指针。 A pointer points to a memory location. 指针指向内存位置。 Any kind of NSObject* or Core Foundation object could be stored there. 任何类型的NSObject *或Core Foundation对象都可以存储在那里。

You need two variables, one for an NSInputStream* and one for a CFReadStreamRef. 您需要两个变量,一个用于NSInputStream *,另一个用于CFReadStreamRef。 Use the appropriate bridge cast to move the NSInputStream* to the CFReadStreamRef. 使用适当的桥接强制转换将NSInputStream *移动到CFReadStreamRef。 Now you have what you want, and can pass the address on. 现在你有你想要的东西,并且可以传递地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM