I am porting an App from Objective-C to Swift and I need to use the following method:
CFStreamCreatePairWithSocketToHost(alloc: CFAllocator!, host: CFString!, port: UInt32,
readStream: CMutablePointer<Unmanaged<CFReadStream>?>,
writeStream: CMutablePointer<Unmanaged<CFWriteStream>?>)
The old logic looks like this (which several web sites seem to agree on):
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(host), port,
&readStream, &writeStream);
NSInputStream inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream outputStream = (__bridge_transfer NSOutputStream *)writeStream;
Which works fine thanks to toll-free bridging. However, ARC does not exist in "Swift-space", and the type system has changed.
How do I turn my streams into instances of
CMutablePointer<Unmanaged<CFReadStream>?>, and
CMutablePointer<Unmanaged<CFWriteStream>?>
And then convert them back into NSStream
subclasses after the CFStreamCreatePairWithSocketToHost
call?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…