NSURLConnection sendSynchronousRequest 不起作用

NSURLConnection sendSynchronousRequest does not work

知道为什么 sendSynchronousRequest 不起作用,而 returns 没有。错误和响应也为零。

let url = NSURL(fileURLWithPath: "http://google.com")!
let ur = NSMutableURLRequest(URL: url)
let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
let errorPtr = NSErrorPointer()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
    let data = NSURLConnection.sendSynchronousRequest(ur, returningResponse: response, error: errorPtr)
    if errorPtr != nil {
        var error: NSError = errorPtr.memory!
    }
})

更新

我试过异步方式:

var oq = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(ur, queue: oq, completionHandler: {response, data, error in

    let ii = 7
})

这里我得到一个错误:

(lldb) po error Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x1567f9d0 {NSErrorFailingURLStringKey=file:///http:/google.com, NSErrorFailingURLKey=file:///http:/google.com, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x15691880 "The requested URL was not found on this server."}

奇怪google不可用?

您正在使用 fileURLWithPath,这将 return URL file:///private/var/folders/xs/6k9v2g217155wr9vgb3xrg_80000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-F81D60A5-6797-4BEB-8AB9-2D156E2B6771/http:/google.com

您需要的是 let url = NSURL(string: "http://google.com")! ,这将 return URL:
http://google.com