我正在尝试使用一段示例代码这篇 NSHipster 文章 http://nshipster.com/ios8/,大约在页面的中间位置。
var inputStream: NSInputStream
var outputStream: NSOutputStream
NSStream.getStreamsToHostWithName(hostname: "nshipster.com",
port: 5432,
inputStream: &inputStream,
outputStream: &outputStream)
我把它和import Foundation
,我收到此错误。
Playground execution failed: error: <REPL>:6:10: error: cannot convert the expression's type 'Void' to type 'String!'
NSStream.getStreamsToHostWithName(hostname: "nshipster.com",
~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这个错误指向第一个参数,它的类型显然是String!
并不是Void
.
我对代码进行了一些更改,以从方法调用中提取定义。这是完整的游乐场:
import Foundation
var inputStream: NSInputStream
var outputStream: NSOutputStream
let host = "nshipster.com"
let port = 5432
NSStream.getStreamsToHostWithName(hostname: host,
port: port,
inputStream: &inputStream,
outputStream: &outputStream)
现在错误指示第三个参数,大概对前两个参数满意。
Playground execution failed: error: <REPL>:10:18: error: cannot convert the expression's type 'Void' to type 'inout NSInputStream'
inputStream: &inputStream,
^~~~~~~~~~~~
我不知道如何提取AutoreleasingUnsafePointer
inputStream 和 outputStream 的变量以相同的方式,但我认为原始示例代码应该可以工作。这是我(和 Mattt)代码中的错误,还是 Swift 中的错误?
EDIT: 我已经提交了拉取请求 https://github.com/NSHipster/articles/pull/174以及 NSHipster 的更正代码。