Convert/cast CFReadStreamRef to NSInputStream (iOS

2019-02-27 12:45发布

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条回答
萌系小妹纸
2楼-- · 2019-02-27 13:22

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

NSInputStream *iStream = objc_unretainedObject(readStream);
查看更多
登录 后发表回答