darksky
darksky

Reputation: 21019

NSUserDefaults: Question about difference between two approaches

Problem

I want to store a NSString in NSUserDefaults and retrieve it later. I have a question about two different retrieving methods. Now at the top of the file I have:

// String used to identify the update object in the user defaults storage.
static NSString * const kLastStoreUpdateKey = @"LastStoreUpdate";

Method 1

NSString *lastUpdate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastStoreUpdateKey];

Method 2

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  
NSString *myString = [prefs stringForKey:kLastStoreUpdateKey];

Are there are significant differences I should know about? Also, can someone please explain what exactly is objectForKey? Apple's API states: that it "Returns the object associated with the first occurrence of the specified default." What exactly do they mean by the "specified default?

Thank you!

Upvotes: 0

Views: 181

Answers (1)

Nishant B
Nishant B

Reputation: 2897

Generally you should use method 1.

that is "objectForKey".

Because, you know that, whatever you have stored in NSUserDefault. So, at the time of retriving it, you can catch the object with proper class like NSString, Array or any other user defined.

genrally "stringForKey" is not used.

If you are storing ingteger, BOOL into NSUserDefault then you should use intForKey, BOOLforKey, etc..

Cheers.

Upvotes: 1

Related Questions