当我运行我的应用程序时,输出显示:
terminating with uncaught exception of type NSException
当我尝试performseguewithidentifier
或UIAlert
时。我正在解析我的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 :(得分:0)
我遇到了同样的问题,使用以下方法解决了我的问题:
DispatchQueue.main.async { [weak self] in
}
我的意思是尝试将警报放在这个区块中。
//希望这会对你有所帮助。