将我的数据保存到 NSUserDefaults 避免重复条目的最佳方法是什么?这是我现在在 viewWillAppear 中所做的事情。我正在存储字典数据的各个条目。我不知道这是否是最好的方法和建议以及如何避免重复的信息。
//
// Keep track of photos that have been viewed by storing the photo data in NSUserDefaults.
//
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *recentlyViewed = [[defaults objectForKey:RECENTLY_VIEWED_KEY] mutableCopy];
if (!recentlyViewed) recentlyViewed = [NSMutableArray array];
[recentlyViewed addObject:self.imageDict];
//
// Keep only MAX number of recently viewed photos.
//
while (recentlyViewed.count > RECENTLY_VIEWED_MAX) {
[recentlyViewed removeObjectAtIndex:0];
}
//
// Write the array back to NSUserDefaults and synchronize it.
//
[defaults setObject:recentlyViewed forKey:RECENTLY_VIEWED_KEY];
[defaults synchronize];
谢谢