當我創建輸出的數據,你可以看到在println(數據),我得到的數據。我試圖從Web服務獲取數據。但是,爲什麼致命錯誤發生時解析數據到JSON.Any幫助???編碼幫助&建議表示讚賞。發現零,而upwrapping一個可選值
import Foundation
protocol AuctionAPIProtocol{
func didReceiveAPIResults(results: NSDictionary)
}
class AuctionAPI{
var delegate: AuctionAPIProtocol
init(delegate: AuctionAPIProtocol){
self.delegate=delegate
}
func get(path:String){
let url = NSURL(string:path)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
println("Task Completed!!!")
if(error != nil){
println(error.localizedDescription)
}
var err: NSError?
println(data)
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
//var jsonResult=JSON(data!)
if(err != nil) {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let results: NSArray = jsonResult["body"] as NSArray
self.delegate.didReceiveAPIResults(jsonResult)
})
task.resume()
}
func searchAuctionLatestFor(){
let urlPath = "http://xxxxxxxxxxxxx/ws/m/automobile/global/latest/search"
get(urlPath)
}
//TODO detail func
//func latestDetail(collectionId: Int){
// get("xxxxxxxxxxxxxxxx")
//}
}
您是否嘗試過使用JSON或使用Advanced Rest Client從服務器獲取數據?高級休息客戶端是鉻的擴展,您可以安裝和調試休息API – Miknash 2015-02-11 09:50:19