0
- (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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mou某
  • 556
  • 3
  • 10
  • 32
  • Have you tried the suggestions in this question - http://stackoverflow.com/questions/4186251/crash-on-deleterowsatindexpaths ? Do you have an exception breakpoint in place? I would have expected that you would get a message about an assertion failure or something similar – Paulw11 Mar 16 '14 at 11:24
  • I removed this line: ` [myFavsTwo removeObjectAtIndex:indexPath.row];` and then deleting gave me this Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' – Mou某 Mar 16 '14 at 12:06
  • @Paulw11 I tried the suggestions on the link you gave me but to no avail – Mou某 Mar 16 '14 at 12:13
  • The message about inconsistency makes sense because you are deleting a row but you haven't changed the underlying data source. Have you tried a [self.tableview beginUpdates] before the removeObject and deleteRows calls and a [self.tableview endUpdates] after? – Paulw11 Mar 16 '14 at 12:22
  • @Paulw11 Yeah, still crashes no errors....I don't know what I did but I fixed it now -- write up an answer and I'll give you the credit – Mou某 Mar 16 '14 at 12:26

1 Answers1

1

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]

Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186