preferences management in IOs with SWIFT

0

Good morning, I'm starting with swift , and I have a question about how to manage preferences.

In android , when I deal with them, if the user has not modified it, I have one by default:

String correo = prefs.getString("email", "[email protected]");

In this way, if nothing is stored, it returns the second value ... well, it is what I would like, if it does not have any value stored by the user, have one by default.

    
asked by Sergio Cv 05.10.2016 в 12:10
source

2 answers

2
let mail = UserDefaults.standard.string(forKey: "mail") ?? "mail@defecto"
    
answered by 05.10.2016 / 17:30
source
1

An easy way is with UserDefaults. The syntax is for Swift 3, it varies for other versions of Swift.

 var email = ""
if let mail = UserDefaults.standard.string(forKey: "mail") {
  // Si existe una clave mail es que se ha creado anteriormente
  // en la variable mail se guarda el valor del contenido
  email = mail
} else { 
  // No existe la clave
  email = "email@pordefecto"  
}
    
answered by 05.10.2016 в 16:18