Oren Edrich Oren Edrich - 1 year ago 93
Swift Question

Reading user defaults with swift

I have a few buttons that set a userdefault to different integers. How would i be able to say "if the userdealts = a specific integer{code}

Here its the code that sets the userdefault:

@IBAction func one(_ sender: AnyObject) {
defaults1.set(1, forKey: "Sphere")
print("Ghost one was selected")

@IBAction func two(_ sender: AnyObject) {
defaults1.set(2, forKey: "Sphere")
print("Ghost two was selected")

Answer Source

you can use:

if defaults1.integer(forKey: "Sphere") == 1 {
  // do something

if defaults1.integer(forKey: "Sphere") == 2 {
  // do something