NSNumber 在 if-else 中被识别为 NSString
我有一段让我抓狂的代码。它的 cellForRowAtIndexPath 显示 4 个属性,其中 2 个是 NSString,2 个是 NSNumber:
//THIS LOGS THE OBJECT BEFORE PROCESSING
NSLog(@"NO DATE EDITING");
NSLog(@"eFK: %@, eFN: %@",editedFieldKey, editedFieldName);
NSLog(@"editedObject vWA is:%@", editedObject);
NSLog(@"Is of type NSString?: %@", ([editedFieldKey isKindOfClass:[NSString class]])? @"Yes" : @"No");
//THIS TESTS TO SEE IF ITS A NSSTRING
if ([editedFieldName isKindOfClass:[NSSTRING class]]) {
NSLog(@"its nsstring");
textField.text = [editedObject valueForKey:editedFieldKey];
NSLog(@"its NOT nsstring");
}
} else {
//ELSE ITS A NUMBER SO GET ITS STRING VALUE BEFORE ASSIGNING
textField.text = [[editedObject valueForKey:editedFieldKey] stringValue];
// And we set the switch to ON if YES, OFF if NO...
if ([editedObject valueForKey:editedFieldKey]) {
NSLog(@"itemreceived equal to 1");
itemReceived.on = YES;
} else {
NSLog(@"itemreceived equal to 0");
itemReceived.on = NO;
}
[textField becomeFirstResponder];
我有 4 个字段,2 个 NSString 和 2 个 NSNumber。当我点击字符串字段时,我得到一个字符串并且工作正常。但是,如果我单击其中一个数字字段,我会得到一个字符串,并且 NSCFNumber isNaturallyRTL...无法识别的选择器发送到实例:
textField.text = [editedObject valueForKey:editedFieldKey];
为什么我的测试失败?为什么即使我选择一个不是字符串的数字,应用程序也会像字符串一样遍历第一个 if 条件?
I have this bit of code thats got me crazy. Its a cellForRowAtIndexPath displaying 4 properties, of which 2 are NSStrings and 2 are NSNumber:
//THIS LOGS THE OBJECT BEFORE PROCESSING
NSLog(@"NO DATE EDITING");
NSLog(@"eFK: %@, eFN: %@",editedFieldKey, editedFieldName);
NSLog(@"editedObject vWA is:%@", editedObject);
NSLog(@"Is of type NSString?: %@", ([editedFieldKey isKindOfClass:[NSString class]])? @"Yes" : @"No");
//THIS TESTS TO SEE IF ITS A NSSTRING
if ([editedFieldName isKindOfClass:[NSSTRING class]]) {
NSLog(@"its nsstring");
textField.text = [editedObject valueForKey:editedFieldKey];
NSLog(@"its NOT nsstring");
}
} else {
//ELSE ITS A NUMBER SO GET ITS STRING VALUE BEFORE ASSIGNING
textField.text = [[editedObject valueForKey:editedFieldKey] stringValue];
// And we set the switch to ON if YES, OFF if NO...
if ([editedObject valueForKey:editedFieldKey]) {
NSLog(@"itemreceived equal to 1");
itemReceived.on = YES;
} else {
NSLog(@"itemreceived equal to 0");
itemReceived.on = NO;
}
[textField becomeFirstResponder];
I have 4 fields, 2 NSStrings and 2 NSNumbers. When I tap on the string fields, I get its a string and it works fine. But if i click on one of the number fields, I get its a string and NSCFNumber isNaturallyRTL...unrecognized selector sent to instance at the:
textField.text = [editedObject valueForKey:editedFieldKey];
Why is my test failing? Why even when I select a number, which is not a string, the app goes thru the first if condition as if it were a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我不得不猜测,我会说你正在测试错误变量的类型。而不是:
仅仅基于您的变量名称(因为您的问题有点缺乏细节),您似乎想要:
我在片段中留下了您的拼写错误 - 但这无法编译。您实际上不是使用
[NSString class]
而不是[NSSTRING class]
吗?注意大小写差异。If I had to guess, I'd say you're testing the type of the wrong variable. Instead of:
Just based on your variable names (as your question is a bit lacking in details), it seems you instead want:
I left your typos in the snippets - this can't be compiling though. Aren't you actually using
[NSString class]
instead of[NSSTRING class]
? Note the capitalization difference.嗯。这只是一个猜测...但首先:
在这里,您查看
editedFieldKey
。在这里,您正在查看
editedFieldName
。在这两种情况下,我看起来好像您正在测试实际值。Hm. This is just a guess... But first:
Here, you look at
editedFieldKey
.Here you are looking at
editedFieldName
. In neither case, I looks like as if your were testing the actual value.