2017-07-10 122 views
0
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" 

       } 

      } 

    } 




} 

從上面的代碼,我得到一個意外地發現零,而解包可選值錯誤。當我刪除!從低調,它給我一個構建錯誤。由於我是一個noob,我不知道解決這個錯誤。Alamofire意外地發現零,同時展開一個可選值

回答

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

....當我使用你的代碼..... <重點:哈希的,任何>旁邊的字典彈出....什麼應給予裏面?....對不起,我只是學習 – nithinsampath

+0

嘗試改變'爲?字典「作爲? [AnyHashable:Any]' –

+0

if(!(jsonData.value(forKey:「nodata」)as!Bool)......我現在只在這條線上得到了那個nil錯誤.....我把這個從互聯網....我甚至不知道這是什麼...你能幫我嗎?.... @ VasiliiMuravev – nithinsampath

相關問題