无法处理Swift中nil值的其他情况

问题描述 投票:0回答:1

在项目中,我需要处理三个条件,

如果成功==“ true”,那么我需要检查下面的条件并相应地移动到该viewcontroller

1)userExists ==“ false”,然后转到ForgotPasswordViewController

2)如果userExists ==“ true” || userID!= nil,然后转到HomeViewController

3)如果userExists ==“ true” || userID == nil然后显示restoreView

以int形式出现在[int int login userid saved Optional("4711fee196114b32bd077aacaea5337f")中,但仍要进入restoreView条件意味着为什么要以userID nil条件进入?

note:userExists ==“ false”条件可以正常工作,但在其他部分却难以解决。.如果有userID,那么还要转到userID == nil条件,为什么?

这里是代码:

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                       
                }
            }
        } 
    }
}

我在编码时出错的地方,请提供代码帮助。

ios swift if-statement null pushviewcontroller
1个回答
0
投票

必须有&&运算符。

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                       
                }
            }

[在其他情况下,每当您具有userExists == "true"时,其中一个条件将处于true状态,||(OR) operator表示if one of the statements is true then go into condition

© www.soinside.com 2019 - 2024. All rights reserved.