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:
Where is HistoryUrl declared?
How can it be modified?
Thanks in advance!
#"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.
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.
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.
Related
I am storing a small dictionary of default values which a user can modify later, but probably will only ever be changed once. NSUserDefaults.standardUserDefaults seems like the right place to store such a thing, my question is: Is there a way to store values at build time instead of runtime? This code seems like it should be unnecessary.
if !NSUserDefaults.standardUserDefaults().dictionaryForKey(default) {
NSUserDefaults.standardUserDefaults().setObject(defaultDictionary, forKey: "default")
}
Or, is there a better alternative I should be considering instead?
Register the default values as described in the documentation
Registering Your App’s Default Preferences
At launch time, an app should register default values for any
preferences that it expects to be present and valid. When you request
the value of a preference that has never been set, the methods of the
NSUserDefaults class return default values that are appropriate for
the data type. For numerical scalar values, this typically means
returning 0, but for strings and other objects it means returning nil.
If these standard default values are not appropriate for your app, you
can register your own default values using the registerDefaults:
method. This method places your custom default values in the
NSRegistrationDomain domain, which causes them to be returned when a
preference is not explicitly set.
When calling the registerDefaults: method, you must provide a
dictionary of all the default values you need to register. Listing 2-1
shows an example where an iOS app registers its default values early
in the launch cycle. You can register default values at any time, of
course, but should always register them before attempting to retrieve
any preference values.
Listing 2-1 Registering default preference values
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register the preference defaults early.
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:#"CacheDataAgressively"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// Other initialization...
}
When registering default values for scalar types, use an NSNumber object to specify the value for the
number. If you want to register a preference whose value is a URL, use
the archivedDataWithRootObject: method of NSKeyedArchiver to encode
the URL in an NSData object first. Although you can use a similar
technique for other types of objects, you should avoid doing so when a
simpler option is available.
To store values at build time, I would make a .plist or a .json file and pre-populate that with the values that you want to store that can then be accessed at runtime.
I am trying to create some settings for my application. One of which is a sort setting which can be 0,1,2. At present I have an enum to handle this:
typedef NS_OPTIONS(NSInteger, SearchSort) {
SearchSortDefaultBestMatched = 0,
SearchSortDistance = 1,
SearchSortHighestRated = 2
};
When a value is selected from a picker I update the [NSUserDefaults standardDefaults] setInteger:
However, when a user first runs the app there is no default set. What I am finding is that if check the [[NSUserDefaults standardUserDefaults] integerForKey:...]; this returns 0. Which would be incorrect. Is this the correct process to store this value/check if the defaults has a value? Ideally I would want nil instead of 0 as 0 would potentially be an option.
Unless the proposed solution is to of course store the values as 1,2,3 and translate back and forth.
NSUserDefaults has a method registerDefaults for this very purpose. you give it a dictionary of "default default" key/value pairs. Those become the initial values that you get when you ask for a given key that has not been set.
I always add my initial default values in the +initialize class method for my app delegate. That method gets called before the system alloc/inits the app delegate, so it gets called before any of your app's custom code (Aside from the code in main) gets called.
EDIT:
As #AdamEberbach suggests in his answer, you can also use objectForKey: instead of integerForKey. That will return nil if no value is stored, or an NSNumber (who's value may be zero) if a value has been stored.
Why not use objectForKey: instead, if you get a valid object then you can cast to NSNumber and get the integerValue: - else you get a nil.
Is it okay to call this?
[[[NSUserDefaults standardUserDefaults] objectForKey:#"searchEnginesOrder"] addObject:#"eBay"];
I have a UITableView whose cells I want to load directly from the defaults, and I also want to modify those defaults. I'm wondering if the above line actually has any effect (when I NSLog the default's array searchEngineOrder it's null, I'm wondering if it's because the above code isn't actually adding to the defaults.
Generally speaking, that line of code would crash. User defaults returns immutable objects so calling addObject: will throw an exception.
You also shouldn't rely on changes made to returned objects being backed into the data store - this isn't user defaults specific, it goes for any API which doesn't document it as supported.
You should be separating your logic between your working data and your stored data with defined modifications and save points. Ensure that you mutable copy the data you extract from user defaults. You should also use registerDefaults: to setup initial values so you don't need to check for existence.
NSUserDefaults returns immutable objects. You have to create mutable copies in order to edit the values.
NSMutableDictionary *subSettings;
subSettings = [[[NSUserDefaults standardUserDefaults]
objectForKey:YOUR_SUBMAP_KEY] mutableCopy];
[subSettings setObject:YOUR_NEW_VALUE forKey:YOUR_VALUE_KEY];
[[NSUserDefaults standardUserDefaults]
setObject:subSettings forKey:YOUR_SUBMAP_KEY];
[subSettings release];
I have a form that I'm creating and to simplify things, I'm trying to create a form field mapper to an object. As such, I create the following dictionary:
self.fieldPropertyMapper = #{
#(CompanyFieldName):self.company,
#(CompanyFieldDescription):self.company.description,
#(CompanyFieldWebsite):self.company.website,
#(CompanyFieldTwitter):self.company.twitter,
#(CompanyFieldAddress):self.company.address,
#(CompanyFieldAddress2):self.company.address2,
#(CompanyFieldCity):self.company.city,
#(CompanyFieldState):self.company.state,
#(CompanyFieldZipcode):self.company.zipcode,
#(CompanyFieldPhone):self.company.phone
};
The keys here are members of the CompanyFieldType enum.
My goal here is to later in my form to assign a value to the returned pointer. Here's what I mean: when a text field in one of my forms stops editing, I'm looking to set the value. Here's what I'd like to accomplish:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CompanyFieldType fieldType = [self fieldTypeForTag:textField.tag];
// Set the value of the respective company property
// In theory it would be something like:
// self.fieldPropertyMapper[#(fieldType)] = textField.text;
}
I'm assuming there's a way to assign by reference but I'm forgetting how to do this. (Is it using the & symbol or **?) I don't remember. Help appreciated! If I'm messing up my terminology, feel free to let me know.
You can't do exactly what you want to do. That is to say, there is no pointer magic that will do what you want.
You can get essentially the same effect, though, with key-value coding. Instead of storing the result of accessing the property (e.g. self.company.website), instead you want to just store the key path to the value you're interested in as a string — e.g. #"company.website". Then you can do like so:
[self setValue:textField.text forKey:self.fieldPropertyMapper[textField.tag]];
Using NSMapTable initialized with NSPointerFunctionsStrongMemory for the keys and NSPointerFunctionsOpaqueMemory for the values. Then you could store the addresses of your iVars backing your properties as the values in the table.
[self.mapTable setObject:&_company forKey:#(CompanyFieldName)];
Haven't tested this but this should get you started.
I'm watching the lesson of standford CS193P, in particular the lecture n°7. I have some doubts about NSUserDefaults. This is the part of code :
#define FAVORITES_KEY #"CalculatorGraphViewController.Favorites"
- (IBAction)addToFavorites:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *favorites = [[defaults objectForKey:FAVORITES_KEY] mutableCopy];
if (!favorites) favorites = [NSMutableArray array];
[favorites addObject:self.calculatorProgram];
[defaults setObject:favorites forKey:FAVORITES_KEY];
[defaults synchronize];
}
I read the documentation about NSUserDefaults, but I don't understand this code in particular [[defaults objectForKey:FAVORITES_KEY] mutableCopy]. FAVORITES_KEY is #"CalculatorGraphViewController.Favorites".
My question is why I should should use CalculatorGraphViewController.Favorites? I don't understand the dot! It seems to me that a structure of getter or setter but Favorites have a capital letter and then CalculatorGraphViewController.Favorites doesn't make sense.
Can You help me please?
You can think of it as a NSDictionary, and the key you provide is only for your own reference. It is for you to retrieve the value back later. You can call it a string like #"CalculatorGraphViewController.Favorites" or any other string that you like. They name it this way just to identify that this is the value for Favorites choices recorded in CalculatorGraphViewController, I believe.
As others have noted, the key is an arbitrary string. The catch is that there can be many parts of your app writing into the defaults. If you picked something really simple as the key, say “favourites”, it could very well happen that two parts of your app would both attempt to use the same key for something different. (Say, for favourite artists and favourite songs.)
This is a common problem in programming, and it’s usually solved by introducing a namespace, or some kind of prefix that makes the clashes less probable. You can often see namespaces in Java classes, something like com.someguy.AppName.SomeClassName. Or even in domain names – like developer.facebook.com and developer.apple.com. Both use the term “developer”, but both differ in the namespace (com.facebook versus com.apple).
You can use the same solution in your use case and introduce a namespace into your defaults key. A logical choice for the namespace is the class name, because you are unlikely to have two classes with the same name. Thus you arrive at the CalculatorGraphViewController prefix. The dot is just a customary way to separate the components in the namespace.
It is just key, it can be any string or object, but not nil