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 } }
Comments
Post a Comment