Toll-free bridging and pointer access in Swift

前端 未结 5 1853
轮回少年
轮回少年 2020-12-13 19:33

I am porting an App from Objective-C to Swift and I need to use the following method:

CFStreamCreatePairWithSocketToHost(alloc: CFAllocator!, host: CFString!         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 20:22

    I wasn't able to get the examples others have provided in this thread to work. Sure, they compiled, but they crashed as soon as the connection was open.

    However, I noticed in the WWDC 2014 discussions (and iOS 8 release notes) that there is a new method for initializing an NSStream for creating a bound pair of in/out streams.

    See below:

    var inputStream: NSInputStream?
    var outputStream: NSOutputStream?
    
    NSStream.getStreamsToHostWithName("localhost", port: 1234, inputStream: &inputStream, outputStream: &outputStream)
    

    This removes the need for the awkward CFStreamCreatePairWithSocketToHost call as well as removing the need for Unmanaged resources.

提交回复
热议问题