How To Save Data For Bookmarks?

泪湿孤枕 提交于 2019-12-10 23:53:27

问题


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 core 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.


回答1:


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];


来源:https://stackoverflow.com/questions/7101319/how-to-save-data-for-bookmarks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!