0

When I run my app the output says:

terminating with uncaught exception of type NSException

when I try to do an performseguewithidentifier or a UIAlert. I am parsing my json data but when I try to either do a UIAlert or a segue when that data equals something I get this error. What am I doing wrong?

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)
    }
}
}
Amit
  • 4,837
  • 5
  • 31
  • 46

1 Answers1

0

I had the same issue and my problem solved by using :

    DispatchQueue.main.async { [weak self] in


   }

I mean try to put your alert in this block.

// Hope this helps you some how.

Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
Swifty Codes
  • 992
  • 10
  • 23