8

I have an object at the root of plist that stores objects and keys (one of which is input from a user textfield). I have no problem writing and creating structure; it's trying to read values that causing problem. Below code I used to write the data, can someone tell me how to read the string [val] and [myKey]?

thank you!

#define defaultValue @"Your Name"
#define myKey @"PersonName"

- (void)textFieldAction:(id)sender {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *val;

    // Get the new value from the textfield

    val = [myTextField stringValue];

    if ([val isEqualToString:defaultValue]) {
        [defaults removeObjectForKey:myKey];
    } else {
        // [defaults setObject:val forKey:myKey];

    NSDictionary *my_dict = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"Name", @"PersonName",
                             @"Email", @"PersonEmail", 
                             @"Dept", @"PersonDept", 
                             @"Job", @"PersonJob", 
                             val, myKey, 
                             @"Status", @"PersonStatus", 
                             nil];

    [defaults setObject: my_dict forKey: @"Person"];

    [[NSUserDefaults standardUserDefaults] registerDefaults:my_dict];

I would like to automatically populate the textfield with the [val], for [myKey], if the user has the info already in the plist. Here's the code I'm trying to use for that:

- (void)applicationDidFinishLaunching:(NSNotification *)notification {

    NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] objectForKey:myKey];
    NSString *val = [defaults stringForKey:myKey];

    if (val == nil) val = defaultValue;
    [myTextField setStringValue:val];
Faiz Fareed
  • 1,498
  • 1
  • 14
  • 31
Devon
  • 127
  • 1
  • 1
  • 7

3 Answers3

34

You can write the value into NSUserDefault like the following:

[[NSUserDefaults standardUserDefaults] setValue:[myTextField stringValue] forKey:@"Person"];

And read it later like the following:

[myTextField setStringValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"Person"];

So you can simplify your code into:

- (void)textFieldAction:(id)sender {
    [[NSUserDefaults standardUserDefaults] setValue:[myTextField stringValue] forKey:@"Person"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

You can set a default value with the registerDefaults:. And when you can retrieve that value simply by calling:

- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
    // ...
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"Person", defaultValue}]
    NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"Person"];
    [myTextField setStringValue:value];
    // ...
}
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
sch
  • 27,436
  • 3
  • 68
  • 83
  • It doesn't like the .text — I get an error "Property 'text' not found on object of type NSTextField *' – Devon Feb 29 '12 at 13:56
  • Yes, I changed that into `setStringValue`when you said it was not an iOS app :) – sch Feb 29 '12 at 13:57
  • AWESOME! works perfectly... thank you so much! ;-) I would up-vote your answer, although I don't have enough clout apparently. Thanks again, truly appreciated. – Devon Feb 29 '12 at 14:18
2

If I understand correctly, you should be able to do what you need by simply doing:

NSString * val = [[NSUserDefaults standardUserDefaults] objectForKey: myKey];
[textfield setText: val];

NSUserDefaults Reference Page

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
  • It outputs the defaultValue "Your Name" in the Textfield that I have defined above, however, if the person has previously entered their name it should read it and populate the text field with that info. Unfortunately it always outputs "Your Name", so it's obviously not getting read correctly, because the name is stored in the plist. – Devon Feb 29 '12 at 13:33
  • It's highly unlikely that it's read incorrectly since its a function straight from the framework. It is much more likely that you are setting it incorrectly. It's hard to say because the code you posted is obviously not the whole code - it woulnd't even compile. My guess is that you are actually setting the value twice - first by [defaults setObject...] and then by registering the dictionary. You should only do it once. – Peter Sarnowski Feb 29 '12 at 13:39
1
myTextField.text = [[NSUSerDefaults standardUserDefaults] objectForKey:myKey];
Adrian Ancuta
  • 1,036
  • 9
  • 16