ios [__NSCFNumber length]: unrecognized selector sent to instance

4.6k Views Asked by At

i have created one custom cell and put button on it and button event is like this

   [cell.BtnRequest addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];

and didtapbutton code is this

 - (void)didTapButton:(id)sender {

    WorkerDetailsVC *wvc=[self.storyboard instantiateViewControllerWithIdentifier:@"workerdetailsvc"];
    NSIndexPath *indepath = [self.tblorderdata indexPathForCell:sender];

    wvc.arrworkarrequest=[arrData objectAtIndex:indepath.row];

    NSLog(@" arr send :%@", wvc.arrworkarrequest);

    wvc.struserid = struserid;

   [self.navigationController pushViewController:wvc animated:YES];

    }

and on the next page i got the array arrworkarrequest and after the load all next page it will show error like this

       2014-10-01 15:08:42.607 demo[2151:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860
       2014-10-01 15:08:42.613 dmeo[2151:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x911a860'
2

There are 2 best solutions below

4
On

Your problem is that sender is of type UIButton not UITableViewCell so it's getting stuck in the [self.tblorderdata indexPathForCell:sender] because that method call expects a type of UITableViewCell.

You need to find the Cell that the button is in. You may be able to do this using a while loop (although it's a bit horrible). Something like:

id obj = sender;
while (![obj isKindOfClass:[UITableViewCell class]]) {
  obj = [sender superview];
}

[self.tblorderdata indexPathForCell:obj];

This feels like it's abusing the view hierarchy though.

2
On

This happens if you compare the length of a string directly

for example

NSString *myString = @"2";

using this statement will generate the error

if(myString.length==2)