swift - UserDefaults checking for nil value not working -


any idea why if statement gives else answer? noticed print object stored "optional()", maybe optional state different nil?

class viewcontroller: uiviewcontroller {      @iboutlet weak var phonelabel: uitextfield!     @iboutlet weak var phonedisplaylabel: uilabel!      @ibaction func storebutton(_ sender: any) {         userdefaults.standard.removeobject(forkey: "nb")         userdefaults.standard.set("\(phonelabel.text!)", forkey: "nb")         print(userdefaults.standard.object(forkey: "nb") any)     }      @ibaction func retrievebutton(_ sender: any) {         let phonenb = userdefaults.standard.value(forkey: "nb")         if let phonenbdisplay = phonenb as? string {             if userdefaults.standard.object(forkey: "nb") != nil {                 phonedisplaylabel.text = "your nb \(phonenbdisplay)"             }             else {                 phonedisplaylabel.text = "enter number first"             }         }     } } 

try this

if (userdefaults.standard.value(forkey: "etc") != nil) {     if (userdefaults.standard.value(forkey: "etc") as! string).character.count == 0     {         //value ""     }else{         //having value     } } 

enter image description here


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -