开发者

How To Save Data For Bookmarks?

I'm creating a very simple bookmarks menu for my app. I just need to save 3 strings for each object.

I was thinking of using core data but I don't want this to be connected with my c开发者_开发问答ore database for various reasons. Therefore what other easy options do I have? NSUserDefaults or .plist?

I just need to save the 3 strings for each object then load them into a table view to be viewed.


I'd recommend NSUserDefaults - is certainly easier. I tend to only use plist files for static data that I want to be editable as the developer, but from the application want it to be read-only (such as coordinates for objects on an embedded map image).

From your description, you would probably want to store an NSArray containing NSDictionary.

// Get the user defaults object
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];


// Load your bookmarks (editable array)
NSMutableArray *bookmarks = [[NSMutableArray alloc]init];
NSArray *bookmarksLoaded = [userDefaults arrayForKey:@"bookmarks"];
if (bookmarksLoaded != nil) {
    [bookmarks initWithArray:bookmarksLoaded];
} else {
    [bookmarks init];
}

// Add a bookmark
NSMutableDictionary *bookmark = [NSMutableDictionary new];
[bookmark setValue:@"value" forKey:@"name"];
[bookmark setValue:@"value" forKey:@"description"];
[bookmark setValue:@"value" forKey:@"code"];
[bookmarks addObject:bookmark];

// Save your (updated) bookmarks
[userDefaults setObject:bookmarks forKey:@"bookmarks"];
[userDefaults synchronize];

// Memory cleanup
[bookmarks release];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜