func getJSON(a:String,i:Int) -> [String]{
let upper = "http://api.themoviedb.org/3/search/movie?query="
let restpart = "&api_key=5565bb94f87424577ef39f24d901ff06"
let newurl = upper + a + restpart
let url = NSURL(string: newurl)
let request = NSURLRequest(URL: url!)
var thetitle: String = "noerror"
var desc: String = "noerror"
var id: String = String(404)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if error == nil{
let swiftyJSON = JSON(data: data!)
thetitle = swiftyJSON["results"][0]["title"].stringValue
desc = swiftyJSON["results"][0]["overview"].stringValue
id = (swiftyJSON["results"][0]["id"].stringValue)
//print(thetitle)
}
else{
thetitle = "error"
desc = "error"
id = "0";
print("there was an error")
}
}
var newFriend = [thetitle,desc,id]
print(thetitle)
task.resume()
return newFriend
}
我想返回newFriend數組,但它似乎總是默認值。我怎樣才能解決這個問題?如何在Swift 2.0中的void函數中返回值?
我認識到任務的返回類型是無效的,這是否意味着我不能更改該塊內的其他代碼?
只需搜索「從dataTaskWithRequest返回值」或「從異步請求返回值」即可。這已被頻繁詢問和回答。 –