尝试执行 UIAlert 或 performSegueWithIdentifier 时以 NSException 类型的未捕获异常终止

terminating with uncaught exception of type NSException when trying to do an UIAlert or a performSegueWithIdentifier

当我 运行 我的应用程序时,输出显示:

terminating with uncaught exception of type NSException

当我尝试执行 performseguewithidentifierUIAlert 时。我正在解析我的 json 数据,但是当我尝试执行 UIAlert 或 segue 时,当该数据等于某个值时,我收到此错误。我做错了什么?

import UIKit

class registerViewController: UIViewController {

@IBOutlet weak var nameInput: UITextField!
@IBOutlet weak var emailInput: UITextField!
@IBOutlet weak var passwordInput: UITextField!
@IBOutlet weak var confirmPassInput: UITextField!
var errorGot = String()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    print(self.errorGot)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func sendBtn(_ sender: UIButton) {

    let name = nameInput.text
    let email = emailInput.text
    let password = passwordInput.text
    let confirmPass = confirmPassInput.text
    if(name?.isEmpty == true || email?.isEmpty == true || password?.isEmpty == true || confirmPass?.isEmpty == true){
        errorMessage(msg: "Alla fält måste vara infyllda!", type: "error")
    }else if(password != confirmPass){
        errorMessage(msg: "Dina lösenord stämmer ej överens", type: "error")
    }else{

    let myUrl = URL(string: "http://mywebsitething.com/wasterace/app/storeData/registration.php?email=\(email!)&name=\(name!)&password=\(password!)")

    var request = URLRequest(url:myUrl!)

    request.httpMethod = "GET"

    let task = URLSession.shared.dataTask(with: request as URLRequest) {data,response,error in

        if(error != nil){
            print("error")
        }
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [Any]
             for jsonResult in json {

                let jsonDict = jsonResult as! [String:String]

                let error = jsonDict["error"]!
                self.printFunc(msg: error)

            }

        } catch {
            print("errorcath")
        }

    }
    task.resume()   
    }
}
func printFunc(msg: String){
    if(msg == "true"){
        self.errorMessage(msg: "Invalid entry", type: "error")
    }else if (msg == "false"){
        self.errorMessage(msg: "You have now registered", type: "success")
    }
}

func errorMessage(msg: String, type: String){

    if(type == "error"){
        let alert = UIAlertController(title: "Error", message: msg, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }else if (type == "success"){
        let alert = UIAlertController(title: "Grattis!", message: msg, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Logga In", style: UIAlertActionStyle.default, handler: { action in
            self.performSegue(withIdentifier: "toLoginRegister", sender: self)
        }))
        self.present(alert, animated: true, completion: nil)
    }
}
}

我遇到了同样的问题,我的问题通过使用解决了:

    DispatchQueue.main.async { [weak self] in


   }

我的意思是尝试将您的警报放在这个区块中。

// 希望这对您有所帮助。