Dani
Dani

Reputation: 1288

iphone development - saving data UI

What is the easiest and simplest way to save a string so that you can use it later on. (DATA persistance)

I've heard of property lists, SQlite3 ..

Upvotes: 0

Views: 69

Answers (3)

Matt Wilding
Matt Wilding

Reputation: 20153

NSUSerDefaults is the easiest and simplest:

// To save...
[[NSUserDefaults standardUserDefaults] setObject:@"my string" forKey:@"someKey"];

// To retrieve...
NSString* recoveredString = [[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"];

Upvotes: 1

Baub
Baub

Reputation: 5044

Well, it depends on what you're going to need to do with it later. I recommend Core Data, to be honest. It may be overweight for what you need, but it is fairly easy once you figure it all out.

Upvotes: 0

jabroni
jabroni

Reputation: 706

If you want something really simple, try NSUserDefaults

-(void)saveToUserDefaults:(NSString*)myString
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:myString forKey:@"Prefs"];
        [standardUserDefaults synchronize];
    }
}

-(NSString*)retrieveFromUserDefaults
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *val = nil;

    if (standardUserDefaults) 
        val = [standardUserDefaults objectForKey:@"Prefs"];

    return val;
}

Please keep in mind, you don't want to be storing a ton of data this way, but if it is a simple string that you need to set and get back, it will get the job done.

Upvotes: 2

Related Questions