I'm trying to save the cell title of a selected table view row to NSUserDefaults. I have the following code in the didSelectRowAtIndexPath: method. At the moment this saves the entire listOfItems (an NSMutableArray). I would like to be able to save and retrieve the listOfItems array object that the user selected in the table row.
thanks for any help
this is the NSMutableArray that is initialized in viewDidLoad:
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:@"Brazil"];
[listOfItems addObject:@"Italy"];
[listOfItems addObject:@"Spain"];
[listOfItems addObject:@"United Kingdom"];
[listOfItems addObject:@"United States"];
the didSelectRowAtIndexPath: method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
// Customize archiver here
[archiver encodeObject:listOfItems forKey:@"keyForYourArrayOfNSIndexPathObjects"];
[archiver finishEncoding];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"keyForYourArrayOfNSIndexPathObjects"];
NSKeyedUnarchiver *unarchiver;
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
[[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
// Customize unarchiver here
listOfItems2 = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
[unarchiver finishDecoding];
NSLog(@"from the list of items: %@", listOfItems2[2]);
}
the NSLog output is: "from the list of items: Spain"