-1

I have question regarding UserDefaults.

Is it possible to store several fields in userdefaults? I am gonna show a sample example below code

fistname = UserDefaults.standard.set(name,forKey: "namekey")
fistnameone = UserDefaults.standard.set(nameone,forKey: "nameonekey")
fistnametwo = UserDefaults.standard.set(nametwo,forKey: "nametwokey")

can I do store many values or userDefaults are supposed to be used for only storing one value?

Manognya
  • 1
  • 2
  • Have you tried running that code and then retrieving the data, to see if it works? – EmilioPelaez Jun 08 '19 at 05:36
  • Actually, in one controller, I have used one value in UserDefaults which was successful. But later if I use again they are giving nil – Manognya Jun 08 '19 at 05:39

2 Answers2

1

Of course you can store many key values data in userDefaults.
The documents https://developer.apple.com/documentation/foundation/userdefaults said it clearly.

UserDefaults
An interface to the user’s defaults database, where you store key-value pairs persistently across launches of your app.
AdamZhang
  • 221
  • 1
  • 3
1

You can create a dictionary for all the values you want to store in UserDefaults. If you want to store structure objects to be saved then please follow below link :

Save Struct to UserDefaults

Hope this helps you in some way.

BhargavR
  • 1,095
  • 7
  • 14