Reputation: 7
I have this code below where I want on the button click add an item (toDoItem is NSString) to an array (toDoitems). This part works. But then I want to save it so that data is available after device restarts. It seemed from other answers that NSUserDefaults is the best way. It compiles but then on the button click it crashes... any hints???
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
XYZAddToDoItemViewController *source = [segue sourceViewController];
XYZToDoItem *item = source.toDoItem;
if (item !=nil) {
[self.toDoitems addObject:item];
[[NSUserDefaults standardUserDefaults] setObject:self.toDoitems forKey:@"itemArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.tableView reloadData];
}
}
Upvotes: 0
Views: 87
Reputation: 4513
A object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData then save it inside your NSUSerDefaults object.
See NSUserDefaults doc
Upvotes: 1