0

I am trying to delete object from a NSMutableArray but still getting the above mentioned error.Here is how I am creating and using the array.

#import "CartViewController.h"
@interface CartViewController ()
{
  NSMutableArray *orderDetails; //my Array

}
@end

-(void)getCart{

SuccessBlock successBlock = ^(NSData *response){

    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
    orderDetails = [[NSMutableArray alloc]init];
    orderDetails = [responseDict objectForKey:@"orderDetails"];
    [self.cartTableView reloadData];

    };

FailureBlock failureBlock = ^(NSError *error){

     NSLog(@"Failure Block %@",[error localizedDescription]);

};
  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@00",BASE_URL,GET_CART]];
  HTTPPostRequest *postReq = [[HTTPPostRequest alloc]initWithURL:url successBlock:successBlock failureBlock:failureBlock];
  [postReq startRequest];
}

Now I am using SWTableViewCell from which I want to delete a particular cell, and hence a entry from array.

 #pragma mark - SWTableViewDelegate
 - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index
 {
  switch (index) {
    case 0:
    {
        NSLog(@"left button 0 was pressed");
       [cell setLeftUtilityButtons:[self leftUndoButton]WithButtonWidth:self.cartTableView.frame.size.width];
       [cell showLeftUtilityButtonsAnimated:YES];

       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [self deleteCell:cell];

        });

     break;
     }
     default:
     {
        break;
     }
   }
}  

-(void)deleteCell:(SWTableViewCell *)cell{

 NSIndexPath *cellIndexPath = [self.cartTableView indexPathForCell:cell];
[orderDetails[cellIndexPath.section] removeObjectAtIndex:cellIndexPath.row];//Exception breakpoint stops here by showing : [__NSDictionaryM removeObjectAtIndex:]: unrecognized selector sent to instance 0x7968afd0
[self.cartTableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationLeft];


 }
iCodes
  • 1,382
  • 3
  • 21
  • 47
  • Log the value of `orderDetails` immediately after `orderDetails = [responseDict objectForKey:@"orderDetails"];` and see what it says. You also don't need to alloc/init the array if the next thing you do is replace it. – Phillip Mills Jan 06 '15 at 14:31
  • First thing to maybe check is what does the code thing that orderDetails is at that point? If you either add a breakpoint there or enable Exception breakpoints, you can ask the debugger what the class of orderDetails is. (Happy to show how if you need). The error is indicating that whatever it is doesn't respond to the method you're calling on it. Since that's definitely a method on mutable arrays it stands to reason it must be a misdirected or nil pointer. – Cocoadelica Jan 06 '15 at 14:31

1 Answers1

3

Here is the cause of the error.

[orderDetails[cellIndexPath.section] removeObjectAtIndex:cellIndexPath.row];

orderDetails is an array and [orderDetails[cellIndexPath.section] is not.

orderDetails[x] is an object which is extracted from the JSON, which in this case is a Dictionary type of Object.

bllakjakk
  • 5,045
  • 1
  • 18
  • 28
  • I changed the line mentioned by you to : [orderDetails removeObjectAtIndex:cellIndexPath.row]; ..However the app crashes on line next to it.And shows lldb .Although I can see the row getting deleted :-) – iCodes Jan 06 '15 at 14:45