雨燕2.0更新之前,此代碼後工作完全從一個PHP腳本的服務器下載我的JSON文件:NSURLConnection的拋出更新到雨燕2.0
let url = NSURL(string: webAdress)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 5.0)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
後更新的Xcode要求我做一些改變。我做了,代碼沒有錯誤,但它總是拋出...
let url = NSURL(string: webAdress)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
let request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 5.0)
var response: NSURLResponse? = nil
var reply = NSData()
do {
reply = try NSURLConnection.sendSynchronousRequest(request, returningResponse:&response)
} catch {
print("ERROR")
}
期待您的解決方案!
打印出來的錯誤?你可以在捕獲物內找到它。 – Rob
print(error)會這樣做 - 不需要聲明一個錯誤變量(只是爲你澄清Rob)。看到我的答案爲Swifty的方式! – Sidetalker