我使用JSON从互联网上提取信息,当我在检查数据后尝试执行segue时,segue崩溃了! 这是我的代码:
passWord = password.text
userName = username.text
var link = "http://ksa-sms.com/SpecialapiK/getUser.php?return=json&username=\(userName!)&password=\(passWord!)"
if let url = NSURL(string: link){
var dataTask = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error : NSError!) -> Void in
if error != nil{
println("Error : \(error.localizedDescription)")
}else{
// println("Data : \(data)")
var jsonError = NSError?()
if let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as? NSDictionary{
println(jsonData)
println(jsonData["Code"]!)
var code = jsonData["Code"]!.integerValue
var userdeta = jsonData["user"]!
if(code != 104){
self.errors.text = "Username or password is wrong !"
}else{
self.errors.text = "Welcome \(self.userName)"
self.userData = AppData(username: self.userName!, password: self.passWord!, phone: userdeta[6]!.integerValue, code: code!)
if(self.userData?.code! == 104){
self.performSegueWithIdentifier("logedIn", sender: sender)
}else{
println("Didn't work !")
println(self.userData?.code!)
}
println(self)
}
}
println(self.userData?.code!)
}
})
dataTask.resume()
}
当我尝试在dataTask.resume()之后执行segue时,应用程序工作正常(但没有信息!),我理解的是dataTask花费了太多时间,因为它在后台执行Xcode编译剩下的代码!我对吗 ?我该如何解决这个问题?
答案 0 :(得分:1)
在主线程上执行segue。
dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.performSegueWithIdentifier("logedIn", sender: nil)
}