Swift NSURLConnection 没有名为 start 的成员

Swift NSURLConnection doesn't have a member named start

 var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
 connection.start()

错误: NSURLConnection 没有名为 start

的成员

有什么想法吗?为什么开始不在这里工作:(

这行不通。

请检查文档:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/index.html#//apple_ref/occ/instm/NSURLConnection/start

特别是

Calling this method is necessary only if you create a connection with the initWithRequest:delegate:startImmediately: method and provide NO for the startImmediately parameter. If you don’t schedule the connection in a run loop or an operation queue before calling this method, the connection is scheduled in the current run loop in the default mode.

为了开始工作,您必须将 startImmediately: true 更改为 startImmediately: false

正如其他张贴者所指出的那样,由于您将 true 作为 startImmediately 传递,您实际上不需要调用 start,但这并不能解释为什么没有找到。

var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)

returns 一个可选的(请求可能无法被 NSURLConnection 处理)所以你必须打开它,任何一个:

connection!.start()

connection?.start()

if let connection = connection {
    connection.start()
}
else {
    // handle error case
}

将为您工作,按 preference/safety 的升序排列。