这是我的代码
let session = NSURLSession.sharedSession()
let url = NSURL(string: "http://localhost:8080/......")
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error ) in
if error != nil{
print("error code = \(error!.code)")
print("error = \(error!)")
}else{
let resultJSON = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
print("data = \(resultJSON)")
}
})
task.resume()
我不知道如何处理try
的{{1}},因为我不知道是否必须从关闭中抛出异常而不是真的我是swift的新手< / p>
答案 0 :(得分:0)
好的,我终于找到了解决方案
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error ) in
if error != nil{
print("error code = \(error!.code)")
print("error = \(error!)")
}else{
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
let arrayJSON = resultJSON as! NSArray
for index in arrayJSON{
print("\(index)")
}
}catch _{
print("received none formatted json")
print("\(data!)")
}
}
})
task.resume()