我寫了這個代碼:無法閱讀的Json iOS版雨燕
func json()
{
let url = NSURL(string: "http://www.educaon.com.br/api/index.php/login?user=" + (username as String))!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) -> Void in
print ("Task Completed")
if let data = data
{
do
{
if let dict: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
{
dispatch_async(dispatch_get_main_queue(), {
self.SenhaJs = dict["SENHA"] as! String
self.IDJs = dict["ID"]as! Int
print (self.SenhaJs) //Resultado Esta Nulo
})
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else if let error = error {
print(error.localizedDescription)
}
}
task.resume()
}
但NSURLSession.sharedSession()
電話後,該應用程序直接轉到task.resume
和不讀取JSON。
任何人都可以幫助我嗎?
你是什麼意思'不讀JSON'? 'dataTaskWithURL'方法運行並返回其他(後臺)線程的結果,所以,當你調試它的步驟到'task.resume'時是正常的 – t4nhpt