开发者

[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 n开发者_开发技巧ot 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!


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


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?


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.


Comment [tempHours release];//it is autoreleased object. You didn't use and with alloc or copy or new:

- (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];
}

more over u have added NSNumber to array so convert to string value:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜