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 proper开发者_如何学运维ty lists, SQlite3 ..
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.
NSUSerDefaults
is the easiest and simplest:
// To save...
[[NSUserDefaults standardUserDefaults] setObject:@"my string" forKey:@"someKey"];
// To retrieve...
NSString* recoveredString = [[NSUserDefaults standardUserDefaults] objectForKey:@"someKey"];
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.
精彩评论