So I have a small project with a table view. Its cells are populated from another view controller that saves the text/date to user defaults.
Everything works, however, I'm looking to sort the table view by the date it was last saved.
How it's declared:
.h
NSMutableArray *dateArray;
.m
self.dateArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"date"];
How can I achieve this? I have seen ways to do it with both an array and mutable, but nothing for a string from userdefaults.
UPDATE w/more info: This is how the date gets saved by the other view controller
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM d, yyyy 'at' h:mm a"];
NSDate *dateNow = [NSDate date];
NSString *dateNowStr = [dateFormatter stringFromDate:dateNow];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSLocale *locale = [NSLocale currentLocale];
[dateFormatter setCalendar:gregorianCalendar];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setLocale:locale];
NSArray *tempDateArray = [[NSUserDefaults standardUserDefaults]objectForKey:@"date"];
NSMutableArray *mutableDateArray = [tempDateArray mutableCopy];
[mutableDateArray insertObject:dateNowStr atIndex:self.index];
[[NSUserDefaults standardUserDefaults] setObject:mutableDateArray forKey:@"date"];
It gets loaded via:
self.dateArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"date"];
Thank You.
NSUserDefaultsfor this sort of storage is convenient, but it’s not really the intended purpose of user defaults. Generally we’d want to save this sort of data in persistent storage (CoreData or plist or JSON or whatever, in the app support directory).