user3306356 user3306356 - 21 days ago 6
iOS Question

Deleting Rows From Table View | NSUserDefaults + NSKeyedArchiver

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
myFavsTwo = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:[[[NSUserDefaults standardUserDefaults] objectForKey:@"MyFavoritez"] mutableCopy]]];
[self.tableView reloadData];
NSLog(@"Number of items in my array is: %d", [myFavsTwo count]);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Number of items in my array is: %d", indexPath.row+1);
[myFavsTwo removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myFavsTwo] forKey:@"MyFavoritez"];
[[NSUserDefaults standardUserDefaults] synchronize];
}


on delete my app keeps crashing at:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver
archivedDataWithRootObject:myFavsTwo] forKey:@"MyFavoritez"];


if I 'continue' on my debugging it still works as it should and everything continues like normal...
not getting any error messages either just a 'break point' message

my view will appear looks like this:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
myFavsTwo = [NSKeyedUnarchiver unarchiveObjectWithData:[[[NSUserDefaults standardUserDefaults] objectForKey:@"MyFavoritez"] mutableCopy]];
[self.tableView reloadData];
NSLog(@"Number of items in my array is: %d", [myFavsTwo count]);
}


and the info is coming from here:

- (void)buttonPressed:(id) sender
{

NSMutableArray *myfavs = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MyFavoritez"]mutableCopy];
if(myfavs != nil)
{
NSLog(@"Array found. Contents: %@",myfavs);
}
else
{
myfavs = [[NSMutableArray alloc] initWithCapacity:0];
}
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"MyFavoritez"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
_myfavs = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
_myfavs = [[NSMutableArray alloc] init];
}

[_myfavs addObject:self.word];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_myfavs] forKey:@"MyFavoritez"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"Number of items in my array is: %d", [_myfavs count]);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


if ([[segue identifier] isEqualToString:@"passFavs"]) {
FavViewController *con = segue.destinationViewController;
con.myFavsTwo = _myfavs; }
}


hope that's not TMI

Answer

Have a look at the advice given in this answer - crash on deleteRowsAtIndexPaths in particular it is good practice to bracket your updates to the table view and your data model with [self.tableView beginUpdates] and [self.tableView endUpdates]