我错过了什么?展开时发现的致命错误意外无效

时间:2015-05-08 08:57:08

标签: ios swift

@IBOutlet var txtusername: UITextField!
@IBOutlet var txtpassword: UITextField!

@IBAction func SigninTapped(sender: UIButton)
{
   var username:NSString = txtusername.text
    var password:NSString = txtpassword.text
  if (self.txtusername.text.isEmpty)
    {

        (UIAlertView(title: "Fields cant be empty", message: "Please Enter email id and password",
            delegate: nil, cancelButtonTitle: "OK").show())
        return
    }
    else if (range==nil)
    {
        (UIAlertView(title: "Invalid Email Id", message: "Please enter correct email id", delegate: nil, cancelButtonTitle: "OK").show())
        return
    }
    else if (txtpassword.text == ""  )
    {

        var alertView:UIAlertView = UIAlertView()
        alertView.title = "Failed!"
        alertView.message = "Please enter Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
        return
    }
    else
    {
        var url:NSURL = NSURL(string:"http://192.168.0.31:8097/api/user/GetValidateUser" + "/" + username + "/" + txtpassword.text)!

        var err:NSError?
        var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type");
        request.addValue("application/json", forHTTPHeaderField: "Accept");
        var reponseError: NSError?
        var response: NSURLResponse?
        var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)


        if ( urlData != nil )
        {
            let res = response as NSHTTPURLResponse!;

            var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!
            NSLog("Response code: %ld", res.statusCode);
            //var   message :String = "Valid user"
            if (res.statusCode == 200)
            {
                if(responseData != "\"Valid user\"")
                {
                    var alertView:UIAlertView = UIAlertView()
                    alertView.title = "Failed"
                    alertView.message = responseData
                    alertView.delegate = self
                    alertView.addButtonWithTitle("OK")
                    alertView.show()
                    return;
                }
                //Get the address book reference
                //GabbersController().GetAddressBook()

                //  var alertView:UIAlertView = UIAlertView()
                //   alertView.title = "Sign in Message"
                //  alertView.message = responseData
                //  alertView.delegate = self
                //  alertView.addButtonWithTitle("OK")
                //  alertView.show()
            }

            else {
                var alertView:UIAlertView = UIAlertView()
                alertView.title = "Failed"
                alertView.message = responseData;
                if let error = reponseError {
                    alertView.message = (error.localizedDescription)
                }
                alertView.delegate = self
                alertView.addButtonWithTitle("OK")
                alertView.show()
            }


        }

        else {
            var alertView:UIAlertView = UIAlertView()
            alertView.title = "Failed"
            alertView.message = "Connection failure";
            if let error = reponseError {
                alertView.message = (error.localizedDescription)
            }
            alertView.delegate = self
            alertView.addButtonWithTitle("OK")
            alertView.show()


        }
        //self.performSegueWithIdentifier:@"Goto__Gabbers" sender:self;

    }


}

对于上面的代码,我得到了突然的错误,因为我认为我可能错过了参考插座删除了现有的并在我的故事板上再次创建我运行应用程序同样的错误投掷没有得到我错过的这里。它在第一次执行得很好,但事实并非如此,投掷致命意外的零错误可以告诉我在这里错过了什么。

这是我得到的确切错误:

  

致命错误:在解包可选值时意外发现nil

发布了我的完整代码

2 个答案:

答案 0 :(得分:3)

根据Swift1.2,您可以执行以下操作。

@IBOutlet var txtusername: UITextField!
@IBOutlet var txtpassword: UITextField!

@IBAction func SigninTapped(sender: UIButton)
{
   if let userName = txtusername.text as? String, let password = txtpassword.text as? String {

    var url:NSURL = NSURL(string:"http://192.168.0.31:8097/api/user/GetValidateUser" + "/" + userName + "/" + password)!
    var err:NSError?
   }
}

答案 1 :(得分:0)

txtusername.texttxtpassword.text此时可能是nil

您必须检查这两个值是否都不为零,然后您可以执行var url:NSURL = NSURL(string:"http://192.168.0.31:8097/api/user/GetValidateUser/\(txtusername.text)/\(txtpassword.text)")!