在打开Optional值时,Alamofire意外地发现了nil

时间:2017-07-10 20:21:44

标签: json swift alamofire

import UIKit
import Alamofire
import AudioToolbox


class LoginVC: UIViewController {

//The login script url make sure to write the ip instead of localhost
//you can get the ip using ifconfig command in terminal
let URL_USER_LOGIN = "http://39.109.246.10/naruvionline/api/login.php"

//the defaultvalues to store user data
let defaultValues = UserDefaults.standard

@IBOutlet weak var hnum: UITextField!

@IBOutlet weak var password: UITextField!

@IBOutlet weak var lblinfo: UILabel!

@IBAction func login(_ sender: UIButton) {

    let parameters: Parameters=[
        "hnum":Int(hnum.text!)!,
        "password":password.text!
    ]

    Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseJSON
        {
            response in
            //printing response
            print(response)

            //getting the json value from the server
            if let result = response.result.value {
                let jsonData = result as! NSDictionary

                if(!(jsonData.value(forKey: "nodata") as! Bool)){
                    let user = jsonData.value(forKey: "patient") as! NSDictionary
                    let hosnum = user.value(forKey: "hnum") as! Int
                    let name = user.value(forKey: "patientname") as! String


                    self.defaultValues.set(hosnum, forKey: "hnum")
                    self.defaultValues.set(name, forKey: "patientusername")


                    let homevc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
                    self.navigationController?.pushViewController(homevc, animated: true)
                    self.dismiss(animated: false, completion: nil)
                }

                else
                {
                    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
                    self.lblinfo.text = "Invalid username or password"

                }

            }

    }




}

从上面的代码中,我在解开Optional值错误时遇到意外发现的nil。当我删除!从垂头丧气,它给了我一个构建错误。由于我是一个菜鸟,我不知道如何解决这个错误。

1 个答案:

答案 0 :(得分:0)

尽可能使用guard

class LoginVC: UIViewController {

    //The login script url make sure to write the ip instead of localhost
    //you can get the ip using ifconfig command in terminal
    let URL_USER_LOGIN = "http://39.109.246.10/naruvionline/api/login.php"

    //the defaultvalues to store user data
    let defaultValues = UserDefaults.standard

    @IBOutlet weak var hnum: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var lblinfo: UILabel!

    @IBAction func login(_ sender: UIButton) {
        guard let hnum = hnum?.text, let password = password?.text else {
            return
        }
        let parameters: Parameters = ["hnum": hnum, "password": password]
        Alamofire.request(URL_USER_LOGIN, method: .post, parameters: parameters).responseJSON { [weak self] (response) in
            guard let strongSelf = self,
                  let jsonData = response.result.value as? [AnyHashable: Any] else {
                return
            }
            if let noData = jsonData["nodata"], noData {
                AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
                self.lblinfo.text = "Invalid username or password"
            }
            guard let user = jsonData["patient"] as? [AnyHashable: Any],
                  let hosnum = user["hnum"] as? Int,
                  let name = user["patientname"] as? String else {
                return
            }
            strongSelf.defaultValues.set(hosnum, forKey: "hnum")
            strongSelf.defaultValues.set(name, forKey: "patientusername")


            guard let homevc = self.storyboard?.instantiateViewController(withIdentifier: "HomeVC") as? HomeVC else {
                return
            }
            strongSelf.navigationController?.pushViewController(homevc, animated: true)
            strongSelf.dismiss(animated: false, completion: nil)
        }
    }
}