ios [NSCFNumber isEqualToString:]: 无法识别的选择器发送到实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6876407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:54:32  来源:igfitidea点击:

[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

objective-ciosnsstringint

提问by ArtSabintsev

Alright, I'm having the following common problem

好的,我遇到了以下常见问题

[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

[NSCFNumber isEqualToString:]: unrecognized selector sent to instance

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

但这次我不知道如何解决它。

Here's the declaration in viewDidLoad:

这是声明在 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 ifstatement)

这是 UIPickerView 的代码方法,其中东西中断(例如,if语句)

- (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:

我想我需要将 NSNumber 对象的 NSArray 类型转换为字符串。我如何用该语句正确地做到这一点:

stringIndex = [self.hours objectAtIndex:row];

stringIndex = [self.hours objectAtIndex:row];

Thanks!

谢谢!

回答by Paul Tiarks

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

回答by MarkPowell

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

您正在返回一个,NSNumber因为这就是self.hours. 正如NSString预期的返回值一样,您应该通过以下方式创建一个字符串:

[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?

或重新评估您的意图。你真的想以这种方式存储索引,还是你想存储NSStrings

回答by jcrowson

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:

如果有人遇到此问题并专门返回行的索引,那么您始终可以通过执行以下操作将 NSNumber 转换为 stringValue:

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

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

stringValue在方法末尾放置 a会将任何内容转换为字符串,您也可以使用intValue.

回答by Vijay-Apple-Dev.blogspot.com

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

注释[tempHours release];//它是自动释放的对象。您没有使用 and 与 alloc 或 copy 或 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:

你已经将 NSNumber 添加到数组中,因此转换为字符串值:

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