Search code examples
iphonexcodehistorynsuserdefaultsfavorites

history/favourite feature iphone


For my current development of iphone app, I want to implement history/favorite feature. After lots of googling I came to conclusion that 'NSUserDefaults' would work better in case of short data storage. But still I am not clear how to interact with NSUserDefaults....Can anyone refer any example or article to work out with this requirement? Help appreciated...

Thanks


Solution

  • First read the NSUserDefaults Class Reference.

    And here it is an example on how to use it:

    // Saving info to defaults
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults setObject:myObj forKey:@"myKey"];
    
    // Retrieving info from defaults
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults objectForKey:@"myKey"];
    

    Depending on the kind of info that you want to store, you may be interested on have a look at NSKeyedArchiver, and use it to encode your objects before save them on the user defaults, or any other place.