我设法成功地将数据保存到 plist(在本例中为书签列表),但我似乎无法弄清楚如何防止用户两次保存相同的数据。我正在使用“moveRowAtIndexPath”重新排序书签。当列表中有重复项时,会在排序过程中导致崩溃。这是我的代码:
- (IBAction)addBookmarkButtonClicked:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Bookmarks.plist"];
NSMutableArray *bookmarksArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == bookmarksArray) {
bookmarksArray = [[NSMutableArray alloc] initWithCapacity:0];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:gameName.text.self forKey:@"gameName"];
[array setObject:gameRating.text.self forKey:@"gameRating"];
[bookmarksArray addObject:array];
[bookmarksArray writeToFile:plistPath atomically: TRUE];
}