无法将表达式的类型'Void'转换为'CFAllocator'类型。

时间:2014-06-03 17:56:08

标签: ios xcode core-foundation swift

var readStream:CFReadStream?
var writeStream:CFWriteStream?
var request: NSMutableURLRequest = NSMutableURLRequest()

var host = request.URL.host
var port = request.URL.port.unsignedIntegerValue

CFStreamCreatePairWithSocketToHost(nil, nil, port, readStream, writeStream)
^ "Cannot convert the expression's type Void to type CFAllocator!"

为什么假设我将它的类型更改为'CFAllocator!'。我尝试更改输入值,但它仍然会出现相同的错误。

1 个答案:

答案 0 :(得分:1)

想出来。

var host: CFString = NSString(string: request.URL.host)
var port: UInt32 =  UInt32(request.URL.port.unsignedIntegerValue)

var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream)

inputStream = readStream!.takeUnretainedValue()
outputStream = writeStream!.takeUnretainedValue()

inputStream!.delegate = self
outputStream!.delegate = self

inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

inputStream!.open()
outputStream!.open()

出现此错误的原因是因为所有值必须是&#34; CreatePairWithSocket ...&#34;的确切类型。功能要求。我做了一些研究,并发现这非常有用question