mardi 26 mai 2020

Unable to handle if else conditions with nil values in Swift

In project i need handle three conditions,

if success == "true" then i need to check below coditions and move to that viewcontroller accordingly

1) userExists == "false" then go to ForgotPasswordViewController

2) if userExists == "true" || userID != nil then go to HomeViewController

3) if userExists == "true" || userID == nil then show restoreView

in print ints coming like this login userid saved Optional("4711fee196114b32bd077aacaea5337f") but still its going to restoreView condition means going to userID nil condition why?

note: userExists == "false" condition working fine but facig touble in else part.. if there is userID then also going to userID == nil condition why?

here is the code:

var savedUserId: String?

var userID: String?


   do{
                    let jsonObject  = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String :AnyObject]
                    print("the json of USERID LOGIN\(jsonObject)")
                    let login = LoginModel.init(fromDictionary: jsonObject)
                    let success    = login.success
                    let userExists = login.userExists
                    self.savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
                    //let userId = self.savedUserId//login.userId

        if success == "true" {


            if userExists == "false" {
                    DispatchQueue.main.async {
                     UserDefaults.standard.set(self.mobileNumTextfield.text!, forKey: "PhoneNumber")
                      UserDefaults.standard.synchronize()
                    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "ForgotPasswordViewController") as! ForgotPasswordViewController
                     viewController.phone = self.mobileNumTextfield.text
                     self.navigationController?.pushViewController(viewController, animated: true)
                    }
            }
            else {
                print("login userid saved \(self.savedUserId)")

                if userExists == "true" || self.userID == self.savedUserId{
                             DispatchQueue.main.async {
                             let viewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
                               self.navigationController?.pushViewController(viewController, animated: true)

                          }
                          }

                if userExists == "true" || self.userID == nil{

                    DispatchQueue.main.async {

                      self.view?.backgroundColor = UIColor(white: 1, alpha: 0.7)
                       self.restoreView.isHidden = false

                    }
                }

        }

        }
    }

where i did mistake in coding, please help with code.

Aucun commentaire:

Enregistrer un commentaire