Convert/cast CFReadStreamRef to NSInputStream (iOS5)

北慕城南 提交于 2019-12-02 04:05:30

问题


I am trying to port my app to iOS5. I am using a TCP connection to a server via CFSockets. My problem now is the conversion (cast) from CFReadStreamRef to NSInputStream (same with write). With iOS4 I could use the toll-free bridging, but with automatic reference counting of iOS5 this isn't possible anymore. This is what I get:

error: Automatic Reference Counting Issue: Cast to 'NSInputStream *' of a non-Objective-C to an Objective-C pointer is disallowed with Automatic Reference Counting

Code:

        CFReadStreamRef readStream;
        CFWriteStreamRef writeStream;

        CFStringRef strRef = CFStringCreateWithCString(NULL,
              [urlStr UTF8String],
              NSUTF8StringEncoding);
        CFStreamCreatePairWithSocketToHost(NULL,
           strRef,
           4444,
           &readStream,
           &writeStream);



        NSInputStream *iStream = (NSInputStream *)readStream;
        NSOutputStream *oStream = (NSOutputStream *)writeStream;         

Is there another way to pipe the socket out/input into an NSStream? Thanks for any hint!


回答1:


Managing Toll-Free Bridging states very clearly that you should use something like this:

NSInputStream *iStream = objc_unretainedObject(readStream);


来源:https://stackoverflow.com/questions/6385093/convert-cast-cfreadstreamref-to-nsinputstream-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!