user1761133
user1761133

Reputation: 13

Valueforkey in NSUserDefaults

I am new to IOS programming and am trying to modify some code a developer wrote for me. I'm having problems in the following code

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
NSString *strUrl=[pref valueForKey:@"HistoryUrl"];

if (strUrl.length>0)
{
    newUrl=strUrl;

}
else
{
    newUrl=@"http://www.google.com";
}

The HistoryUrl parameter seems to have the value 'http://www.yahoo.com' stored in it. I've looked everywhere and searched the net on how to replace this value to google's address. I have even gone through all the code in XCode and can't find where historyurl is declared:

  1. Where is HistoryUrl declared?
  2. How can it be modified?

Thanks in advance!

Upvotes: 1

Views: 807

Answers (3)

Caleb
Caleb

Reputation: 125037

Where is HistoryUrl declared?

It's not. @"HistoryUrl" is just a string. Read up on NSUserDefaults to learn how it all works, but in a nutshell the defaults system is like an associative array (also known as a dictionary or map), where you can key/value pairs. In this case, @"HistoryUrl" is the key, and @"http://www.yahoo.com" is the value. You can make up whatever keys you want for storing your values.

How can it be modified?

Do you want to modify the key, or the value associated with the key? If the former, just make up a different key and use it. If the latter, use the methods of NSUserDefaults to set a different value:

NSUserDefaults *pref=[NSUserDefaults standardUserDefaults];
[pref setObject:@"http://google.com" forKey:@"HistoryUrl"];
[pref synchronize];

Note: the -synchronize call isn't strictly necessary, as the system will generally write your change eventually. But a lot of people like to call it whenever they make a change to the defaults system.

Upvotes: 0

gnasher729
gnasher729

Reputation: 52632

@"HistoryUrl" is an NSString* containing the string HistoryURL. Thats' how you write an NSString* with fixed data.

pref is an object representing the user's preferences. The user's preference contain multiple key - value pairs. For example there might be a key named "HistoryUrl" which might have some value.

The valueForKey: method reads the value that is stored under the key "HistoryUrl" and stores it into strUrl. If there is no key named "HistoryUrl" then the result will be nil. (The use of valueForKey: is strange, because it is not a method of NSUserDefaults itself; typically one would use objectForKey:)

The following code checks whether the value read has any characters in it (length is roughly speaking the number of characters); if there are any characters then newUrl is set to that value; if there were no characters then newUrl is set to the NSString* "http:/www.google.com".

So someone at some time has stored a value under the name "HistoryUrl" into the application's preference file. You remove that value by calling

[pref removeObjectForKey:@"HistoryUrl"]

Or, since you don't seem to want anything other than "google", remove all the code and just write NSString* newUrl = @"http://www.google.com" if that's what you want.

Upvotes: 1

Alex
Alex

Reputation: 8991

HistoryURL is an arbitrary key and it is being used in your code to retrieve a value from NSUserDefaults. At some stage in your code, you will want to use setObject:forKey: to update the value stored in NSUesrDefaults. You will also need to call synchronize to save the new value after it has been set.

Upvotes: 1

Related Questions