Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

Alright, I'm having the following common problem

[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

but this time I'm not sure how to fix it.

Here's the declaration in viewDidLoad:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableArray *tempHours = [NSMutableArray array];
    for (int i = 0; i < 12; i++) { 
        [tempHours addObject:[NSNumber numberWithInt:(i+1)]];
    }
    self.hours = tempHours; // 'hours' is a synthesized NSArray property of the ViewController
    [tempHours release];

  // two more similar array declarations here

}

Here's the code method of the UIPickerView where stuff breaks (e.g., the if statement)

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    NSString *stringIndex = [NSString stringWithFormat:@"Row #%d", row];

    if(component == 0) {
        return stringIndex = [self.hours objectAtIndex:row];
    }

    // more code for other components (of the same form) here

    return stringIndex;
}

I think I need my NSArray of NSNumber objects to be type-casted as strings. How do I do that properly with that statement:

stringIndex = [self.hours objectAtIndex:row];

Thanks!

like image 670
ArtSabintsev Avatar asked Jul 29 '11 16:07

ArtSabintsev


3 Answers

return [NSString stringWithFormat:@"%@",[self.hours objectAtIndex:row]];
like image 158
Paul Tiarks Avatar answered Nov 13 '22 20:11

Paul Tiarks


You are returning an NSNumber as that is what is held in self.hours. As NSString is the expected return value you should create a string via:

[NSString stringWithFormat:@"%@", [self.hours objectAtIndex:row]];

or reevaluate your intent. Did you actually want to store indices in this way, or did you want to store NSStrings?

like image 8
MarkPowell Avatar answered Nov 13 '22 20:11

MarkPowell


If anyone is having this problem and specifically returning an index of a row, then you can always convert the NSNumber to a stringValue by doing the following:

NSString *code = [[[JSONResponse objectForKey:@"meta"] objectForKey:@"code"] stringValue];

Placing a stringValue at the end of the method will convert anything to a string, you can also use intValue.

like image 3
jcrowson Avatar answered Nov 13 '22 19:11

jcrowson