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

发布于 2024-11-27 00:56:22 字数 1137 浏览 0 评论 0原文

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

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

但这次我不知道如何修复它。

这是 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

}

这是 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;
}

我想我需要将 NSNumber 对象的 NSArray 类型为 -铸造为字符串。如何使用该语句正确执行此操作:

stringIndex = [self.hours objectAtIndex:row];

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中二柚 2024-12-04 00:56:22
return [NSString stringWithFormat:@"%@",[self.hours objectAtIndex:row]];
return [NSString stringWithFormat:@"%@",[self.hours objectAtIndex:row]];
沧桑㈠ 2024-12-04 00:56:22

您将返回一个 NSNumber,因为它是 self.hours 中保存的内容。由于 NSString 是预期的返回值,您应该通过以下方式创建一个字符串:

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

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

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?

北凤男飞 2024-12-04 00:56:22

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

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

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

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.

拍不死你 2024-12-04 00:56:22

Comment [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];
}

更重要的是您已将 NSNumber 添加到数组
所以
转换为字符串值:

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

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]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文