[NSCFNumber isEqualToString:]:无法识别的选择器发送到实例
好吧,我遇到了以下常见问题
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将返回一个
NSNumber
,因为它是self.hours
中保存的内容。由于NSString
是预期的返回值,您应该通过以下方式创建一个字符串:或重新评估您的意图。您实际上想以这种方式存储索引,还是想存储
NSStrings
?You are returning an
NSNumber
as that is what is held inself.hours
. AsNSString
is the expected return value you should create a string via:or reevaluate your intent. Did you actually want to store indices in this way, or did you want to store
NSStrings
?如果有人遇到此问题并且专门返回行的索引,那么您始终可以通过执行以下操作将 NSNumber 转换为 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:
Placing a
stringValue
at the end of the method will convert anything to a string, you can also useintValue
.Comment
[tempHours release];
//它是自动释放的对象。您没有使用 and 与 alloc 或 copy 或 new:更重要的是您已将 NSNumber 添加到数组
所以
转换为字符串值:
Comment
[tempHours release];
//it is autoreleased object. You didn't use and with alloc or copy or new:more over u have added NSNumber to array
so
convert to string value: