Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating an NSArray when NSUserDefaults is updated`
    text
    copied!<p>I have an application that pulls a list of the top places on flickr and returns the recent photos posted to those places. That is all done dynamically. I am also storing a list of recent images the user has viewed to NSUserDefaults. The data that flickr needs in order to pull up an image is stored in an NSDictionary, so I am writing the NSDictionary to an array, and then storing said array in NSUserDefaults. This is for a learning project, so I'm required to use NSUserDefaults, even if there is likely a better way. </p> <p>At this point, ALMOST everything works great. However, I am flummoxed on a method of updating the Recent Images after a user views a new image in the dynamic views. NSUserDefaults updates without issue, and the additional recent images show if I shut down the app and restart it. </p> <p>Here is my code where I write to defaults. Doesn't seem to be an issue here. </p> <pre><code>idvc = [[ImageDisplayViewController alloc] init]; idvc.flickrInfo = [placePhotosArray objectAtIndex:[indexPath row]]; // Add data to NSUserDefaults NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults]; NSData *dataFromRecentArray = [currentDefaults objectForKey:@"recentImages"]; if (dataFromRecentArray != nil) { NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromRecentArray]; if (oldArray != nil) { currentRecentImagesArray = [oldArray mutableCopy]; } else { currentRecentImagesArray = [[NSMutableArray alloc] init]; } } else { currentRecentImagesArray = [[NSMutableArray alloc] init]; } [currentRecentImagesArray addObject:idvc.flickrInfo]; [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:currentRecentImagesArray] forKey:@"recentImages"]; [currentRecentImagesArray release]; [self.navigationController pushViewController:idvc animated:YES]; [idvc release]; </code></pre> <p>Here is where I am reading the data back out</p> <pre><code>- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults]; NSData *dataFromRecentImages = [currentDefaults objectForKey:@"recentImages"]; if (dataFromRecentImages != nil) { NSArray *currentArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromRecentImages]; recentImagesArray = [[NSArray alloc] initWithArray:currentArray]; } } </code></pre> <p>Honestly, the requirements for the assignment are fulfilled, I could stop here if I wanted to. But I'd like to understand how this is done. I am pretty good at extrapolating generic code examples and applying it to mine, so you don't have to fix MY specific problem. Just need to know how to update a variable that's value is set from user defaults after defaults have been updated. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload