我正在将一个应用程序从 Objective-C 移植到 Swift,我需要使用以下方法:
CFStreamCreatePairWithSocketToHost(alloc: CFAllocator!, host: CFString!, port: UInt32, \
readStream: CMutablePointer<Unmanaged<CFReadStream>?>, \
writeStream: CMutablePointer<Unmanaged<CFWriteStream>?>)
旧的逻辑看起来像这样(几个网站似乎都同意):
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;
由于免费桥接,这工作得很好。但是,“Swift-space”中不存在 ARC,类型系统也发生了变化。
如何将我的流转换为
CMutablePointer<Unmanaged<CFReadStream>?>, and
CMutablePointer<Unmanaged<CFWriteStream>?>
然后在调用后将它们转换回NSStream
子类CFStreamCreatePairWithSocketToHost
?