2017-08-31 77 views
0

时类型NSException的未捕获的异常终止:尝试当我运行我的应用程序输出说做一个UIAlert或performSegueWithIdentifier

terminating with uncaught exception of type NSException

当我尝试做一个performseguewithidentifierUIAlert。我解析的json数据,但是当我尝试要么做一个UIAlert或赛格瑞时数据等于什么我得到这个错误。我究竟做错了什么?

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) 
    } 
} 
} 
+0

不要你有更多关于NSException“类型NSException的未捕获的异常终止”?通常有一个明确的信息。 – Larme

+0

设置一个异常断点,看看具体的错误 –

+0

显示控制台错误 –

回答

0

我有同样的问题,我的问题通过解决:

DispatchQueue.main.async { [weak self] in 


    } 

我的意思是试图把您的警报在此块。

//希望这有助于你一些如何。

相关问题