Ios NSURLConnection sendSynchronousRequest不工作

Ios NSURLConnection sendSynchronousRequest不工作,ios,nsurlconnection,Ios,Nsurlconnection,知道为什么sendSynchronousRequest不起作用,并返回nil。错误和响应也是零 let url = NSURL(fileURLWithPath: "http://google.com")! let ur = NSMutableURLRequest(URL: url) let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil let errorPtr = NSErrorPointer()

知道为什么
sendSynchronousRequest
不起作用,并返回nil。错误和响应也是零

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!
    }
})
这里我得到一个错误:

(lldb)采购订单错误 Error Domain=NSURERRORDOMAIN Code=-1100“在此服务器上找不到请求的URL。”UserInfo=0x1567f9d0{NSERRORFAILLINGURLSTRINGKEY=file:///http:/google.com,n错误失败键=file:///http:/google.com,NSLocalizedDescription=在此服务器上找不到请求的URL.,NSUnderlyingError=0x15691880“在此服务器上找不到请求的URL。”}


奇怪的谷歌不可用?

您正在使用
fileURLWithPath
,这将返回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(字符串:http://google.com")!
这将返回URL:

当您可以轻松使用
sendSynchronousRequest:
时,为什么要在
dispatch\u async
中使用
sendSynchronousRequest:
?定义“不起作用”。没有
println()
调试调用,您是否在
sendSynchronousRequest
调用后设置断点并检查结果?在编写代码时,最好的方法是假设它是刚刚编写的代码,而不是系统。这对我来说已经运行了几十年。此外,仔细阅读错误消息通常会提供这是一个好信息,在本例中为:“NSErrorFailingURLKey=file:///http:/google.com“是现场。我也经常意识到我犯了错误,而不是系统:)在我的情况下,错误在99%以上的时间里是我的。;-)
var oq = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(ur, queue: oq, completionHandler: {response, data, error in

    let ii = 7
})