3

I have looked everywhere and cannot find an explanation of how to use registerDefaults().

Every post that asks about how to set defaults is answered by "use registerDefaults()", the small problem I'm having with this is I have no clue how to use registerDefaults().

Could someone show me exactly what code to write and where to put it?

My problem: I have 5 values (Int) that need to have a specific default but can later be changed by the user. I have checked Xcode documentation, youtube and google, can't find an answer.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Jack Hayton
  • 363
  • 4
  • 18
  • This is Objective-C but it took me 10 seconds to find [this](http://stackoverflow.com/questions/19972367/how-does-nsuserdefaults-registerdefaults-work)......... – somtingwong Aug 14 '15 at 12:52
  • I don't know Objective-C, and I'm very new to programming that a lot of this looks like gibberish to me... Im so stuck with this I need to find a answer for swift so I can literally copy paste it in to my app, and then understand how it works. Im just so new to this that a lot of the explanations don't make sense because I don't know what they are talking about. Thanks anyway. – Jack Hayton Aug 14 '15 at 12:55
  • Based on the comment you posted to the answer below I really suggest you start with the basics. Copy and pasting code like this and trying to understand when you don't even know the basics will get you nowhere. – somtingwong Aug 14 '15 at 13:02
  • 1
    I know, but i am trying my best to read up on everything, however with programming often the best way for me to learn is to see the answer, then I look at it and understand how/why it works, I don't just copy and paste and leave it. I make sure I understand it. I find it easier to work backwards, start with the answer then figure out why it works. Im teaching myself this from home so I have no one to ask for help but you guys, I've been stuck on this for days and literally cannot do it. I just want to know the answer so i can figure it out myself. – Jack Hayton Aug 14 '15 at 13:08

2 Answers2

8

Just pass the dictionary of key-value pairs:

NSUserDefaults.standardUserDefaults().registerDefaults([
    "SomeKey" : 123,
    "SomeArrayKey" : [1, 2, 3, 4, 5]
])
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • Thank you! If I had a variable in my app called "test" how would I make that equal to the default "somekey" you had put above? If my variable "test" would need to equal "123". And where in my Xcode files do I put this piece of code? – Jack Hayton Aug 14 '15 at 12:57
  • you should put it viewController.swift – SwiftStudier Aug 14 '15 at 13:07
  • Ok, I have done this with my code. I have set the key value pairs for the defaults i need set, I am still so confused as how to use this though. How do I get my variable "test" to correspond with the value? I am so stuck – Jack Hayton Aug 14 '15 at 13:18
  • Using registerDefaults simply sets up a starting value for a certain key/value pair, so if your code never called `setObject:forKey:`, it would still have a value. – Duncan C Aug 14 '15 at 13:31
  • 1
    Then if you need a value from defaults, just load it: `let aVar = NSUserDefaults.standardUserDefaults().integerForKey: "SomeKey"` – Duncan C Aug 14 '15 at 13:33
6

@Kirsteins already posted the code, but here is the idea.

RegisterDefaults creates starting values for one or more keys in NSUserDefaults, "default defaults", if you will.

You want to call registerDefaults very early in the invocation of your program so that you are sure the defaults will be registered before you try to read any values from defaults.

I usually put them in the initialize (class) method of my app delegate, since the initialize method gets called before the app delegate is even instantiated.

Just create a dictionary of the key/value pairs that you want as default values, and call NSUserDefaults.standardUserDefaults().registerDefaults.

Putting it all together, your code (in your app delegate class) might look like this:

class func initialize()
{
  let initialDefaults: NSDictionary = 
    ["firstKey": true,
     "answerToLifeKey": 42,
     "airspeedVelocityKey": "European or African"
    ]
  NSUserDefaults.standardUserDefaults().registerDefaults(initialDefaults)
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Ok, thank you for clearing that up. So how do I actually use those values though? In my case, I have a view called settings, here there are text fields for the user to change the default settings. These settings are used mathematically later on (multiplied etc..). So how do I make my variable equal to the default value? If my dictionary is ["variableOne" : 5 ] Do I use this? variableOne = testOne.integerForKey("variableOne"). (testOne is a variable declared as NSUserDefaults.standardUserDefaults() ) I still don't understand how the code above gives me a variable that I can use to multiply etc. – Jack Hayton Aug 14 '15 at 13:47
  • Sorry, just saw your follow-on. Yes, once you use the above, you just fetch values from userDefaults as normal, and you'll get the value set in the `registerDefaults` call if you haven't written a different value to userDefaults. – Duncan C Feb 19 '17 at 13:11