2
將數據保存到NSUserDefaults以避免重複條目的最佳方法是什麼?這是我現在正在做的事情。我正在存儲單個詞條數據的條目。我不知道這是否是最好的方式和建議,以及有關如何避免dups的信息。存儲NSUserDefaults時避免重複(Xcode)
//
// 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];
感謝