Lir*_*ivo 34 session json http ios swift
按下Submit按钮时,我在loginViewController中调用了这个调用:
let http = HTTPHelper()
http.post("http://someUrl.com/Login/userEmail/\(username.text)/Pswd/\(userPass.text)", postCompleted: self.checkLogin)
Run Code Online (Sandbox Code Playgroud)
而我发送的checkLogin函数只执行:
func checkLogin(succeed: Bool, msg: String){
if (succeed){
self.performSegueWithIdentifier("logInTrue", sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
post函数是HTTPHelper类是:
func post(url : String, postCompleted : (succeeded: Bool, msg: String) -> ()) {
var request = NSMutableURLRequest(URL: NSURL(string: url)!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var err: NSError?
self.task = session.dataTaskWithURL(NSURL(string: url)!) {(data, response, error) in
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments, error: &err) as? NSDictionary
var msg = "No message"
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console
if(err != nil) {
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
postCompleted(succeeded: false, msg: "Error")
}
else {
// The JSONObjectWithData constructor didn't return an error. But, we should still
// check and make sure that json has a value using optional binding.
if let parseJSON = json {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
if let success = parseJSON["result"] as? Bool {
postCompleted(succeeded: success, msg: "Logged in.")
}
return
}
else {
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
postCompleted(succeeded: false, msg: "Error")
}
}
}
self.task!.resume()
}
Run Code Online (Sandbox Code Playgroud)
当成功调用checkLogin函数时:true它无法执行SegueWithIdentified函数..错误如下所示:
在声明失败 - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished],2014年11月15日/SourceCache/UIKit_Sim/UIKit-3318.16.14/Keyboard/UIKeyboardTaskQueue.m:374 17:41:29.540 Wavesss [8462:846477]***终止应用程序由于未捕获的异常"NSInternalInconsistencyException",原因是:" - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished]只能从主线程调用."
请帮助我,虽然我很难找到解决方案,看起来我不能在视图控制器之间传递,而url任务仍在其他线程上执行.先谢谢你们!
Nat*_*ook 64
您的checkLogin函数正在另一个线程上调用,因此您需要切换回主线程才能调用self.performSegueWithIdentifier.我更喜欢使用NSOperationQueue:
func checkLogin(succeed: Bool, msg: String) {
if (succeed) {
NSOperationQueue.mainQueue().addOperationWithBlock {
self.performSegueWithIdentifier("logInTrue", sender: self)
}
}
}
Run Code Online (Sandbox Code Playgroud)
备选:xCode 10.1 1/2019
func checkLogin(succeed: Bool, msg: String) {
if (succeed) {
OperationQueue.main.addOperation {
self.performSegue(withIdentifier: "logInTrue", sender: self)
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 16
使用Xcode 8.0和Swift 3,它已被修改为以下构造:
OperationQueue.main.addOperation{
<your segue or function call>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17055 次 |
| 最近记录: |