gpt4 book ai didi

ios - swift 如何处理 session 中的 nsjson

转载 作者:行者123 更新时间:2023-11-30 14:05:15 26 4
gpt4 key购买 nike

这是我的代码

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()

我不知道如何处理 NSJSONSerialization.JSONObjectWithDatatry 因为我不知道是否必须从 not 的闭包中抛出异常我确实是 Swift 的新手

最佳答案

好吧,我终于找到了解决办法

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()

关于ios - swift 如何处理 session 中的 nsjson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32513550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com