You need to store the data in a persistent data store if you want to persist between launches. UserDefaults is a simple solution, and you can use the registerDefaults method to seed it.
Register your default properties. In AppDelegate.didFinishingLaunching may be a good place
UserDefaults.standard.register(defaults: [
"MyKey" : String(arc4random_uniform(10000))
])
Pull out the value:
let stranger = UserDefaults.standard.string(forKey: "MyKey")
If you don't want to register the defaults on app start, or need to change it, then you can also set the property when you first need it, using an if let check to see if it already exists, and if not, set it.
func getStranger() -> String {
if let v = UserDefaults.standard.string(forKey: "MyKey") {
return v
} else {
let rand = String(arc4random_uniform(10000))
UserDefaults.standard.set(rand, forKey:"MyKey")
return rand
}
}
Note: Code written in SO answer box, not tested and may contain syntax errors. Demo purposes only