当按下提交按钮时,我的LoginView控制器中有此调用:

let http = HTTPHelper()
    http.post("http://someUrl.com/Login/userEmail/\(username.text)/Pswd/\(userPass.text)", postCompleted: self.checkLogin)

while the checkLogin function I send is only performing:

func checkLogin(succeed: Bool, msg: String){
    if (succeed){
        self.performSegueWithIdentifier("logInTrue", sender: self)
    }
}

the post function is HTTPHelper class is :

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

when the checkLogin function is called with success: true it fails to performSegueWithIdentified function .. the error looks like:

Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-3318.16.14/Keyboard/UIKeyboardTaskQueue.m:374 2014-11-15 17:41:29.540 Wavesss[8462:846477] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished] may only be called from the main thread.'

请帮助我,尽管我很难找到解决办法,

推荐答案

您的checkLogin函数正在另一个线程上被调用,因此您需要切换回主线程,然后才能调用self.performSegueWithIdentifier.我更喜欢使用NSOperationQueue:

func checkLogin(succeed: Bool, msg: String) {
    if (succeed) {
        NSOperationQueue.mainQueue().addOperationWithBlock {
            self.performSegueWithIdentifier("logInTrue", sender: self)
        }        
    }
}

Alternate: xCode 10.1 1/2019

func checkLogin(succeed: Bool, msg: String) {
    if (succeed) {
        OperationQueue.main.addOperation {
            self.performSegue(withIdentifier: "logInTrue", sender: self)
           }        
      }
 }

Json相关问答推荐

如何用JQ更改空/布尔/数字的 colored颜色 ?

Pandas 对REST API的自定义响应

递归解码嵌套列表(具有任意深度的列表列表)

jq:将一个数组与多个数组连接起来

使用 jq 如何更改键的值?

在Databricks中如何将JSON文件作为字典读取

在 NX 工作区中跨多个应用共享 ngx-translate 翻译文件

在 CodePipeline 中调用 lambda 时传递用户参数

Powershell 7.2:ConvertFrom-Json - 日期处理

jq:来自嵌套 JSON 的映射

根据数据框中的其他列值将列表 json 对象插入行

如何将从嵌套 Select 返回的空值转换为空数组?

使用 JQ 从文件中删除重复的 JSON 块

如何用 Xidel 正确读取这个 JSON 文件?

如何在 Eclipse 中安装和使用 JSON 编辑器?

我们可以使用 JSON 作为数据库吗?

有什么方法可以在 elasticsearch 服务器中导入 json 文件(包含 100 个文档).?

带有方法参数的 WCF webHttpBinding 错误. 最多可以在没有包装元素的情况下序列化一个主体参数

JSON Schema:验证数字或空值

使用 JSON.NET 序列化/反序列化对象字典