2

I keep getting a unrecognized selector when I try to add an object to my mutable array, I'm trying to add and remove items to a mutable array when a cell is selected in my tableview, here is my code:

@interface SomeViewController
  @property (nonatomic, strong) NSMutableArray *selectedItems;
@end

View did load:

-(void)viewDidLoad {

    [super viewDidLoad];

    self.selectedItems = [[NSMutableArray alloc] init];
}

Selecting cell:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    [self.selectedItems addObject:[NSString stringWithFormat:@"%@", cell.contactID]];

Contact Id attribute on MyCell file is:

@property (strong, nonatomic) NSString *contactID;

I keep getting this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM _fastCStringContents:]: unrecognized selector sent to instance 0x7fc3dd1091b0'
jckly
  • 831
  • 1
  • 11
  • 22
  • Why you use `[self.selectedItems addObject:[NSString stringWithFormat:@"%@", cell.contactID]]`; instead of `[self.selectedItems addObject:cell.contactID];?` Try to change it – iSashok Jun 10 '16 at 07:53
  • I looked at some questions on here and saw that it might help. So I tried it out. I've tried both variations you suggested and both return the exact same error I'm getting. – jckly Jun 10 '16 at 07:57
  • `contactID` is not string every time in your case. try to debug your cell data. add an `exception breakpoint`. – Blind Ninja Jun 10 '16 at 08:04

3 Answers3

8

Somewhere you have an object that you believe is a string, when in reality it is a mutable array - that's what the error message says. Set a breakpoint on exceptions, that should tell you exactly where it happens, and then find the array that you believe is a string.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

Try to implement it in normal UITableView. If you get no issue then its problem to yours MyCell. Check with mutable or copy array also. Try to NSLog the cell.contactID

Jamshed Alam
  • 12,424
  • 5
  • 26
  • 49
0

It could be when you pass array directly into NSLog, like NSLog(array), instead of NSLog(@"%@", array)

Alex Nazarov
  • 1,178
  • 8
  • 16