NSNumber 在 if-else 中被识别为 NSString

发布于 2024-12-20 05:30:55 字数 1358 浏览 2 评论 0原文

我有一段让我抓狂的代码。它的 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 技术交流群。

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

发布评论

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

评论(2

ぃ双果 2024-12-27 05:30:55

如果我不得不猜测,我会说你正在测试错误变量的类型。而不是:

...
if ([editedFieldName isKindOfClass:[NSSTRING class]]) {
...

仅仅基于您的变量名称(因为您的问题有点缺乏细节),您似乎想要:

...
if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSSTRING class]]) {
...

我在片段中留下了您的拼写错误 - 但这无法编译。您实际上不是使用 [NSString class] 而不是 [NSSTRING class] 吗?注意大小写差异。

If I had to guess, I'd say you're testing the type of the wrong variable. Instead of:

...
if ([editedFieldName isKindOfClass:[NSSTRING class]]) {
...

Just based on your variable names (as your question is a bit lacking in details), it seems you instead want:

...
if ([[editedObject valueForKey:editedFieldKey] isKindOfClass:[NSSTRING class]]) {
...

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.

弄潮 2024-12-27 05:30:55

嗯。这只是一个猜测...但首先:

NSLog(@"Is of type NSString?: %@", ([editedFieldKey isKindOfClass:[NSString class]])? @"Yes" : @"No");

在这里,您查看 editedFieldKey

if ([editedFieldName isKindOfClass:[NSSTRING class]]) {

在这里,您正在查看editedFieldName。在这两种情况下,我看起来好像您正在测试实际值。

Hm. This is just a guess... But first:

NSLog(@"Is of type NSString?: %@", ([editedFieldKey isKindOfClass:[NSString class]])? @"Yes" : @"No");

Here, you look at editedFieldKey.

if ([editedFieldName isKindOfClass:[NSSTRING class]]) {

Here you are looking at editedFieldName. In neither case, I looks like as if your were testing the actual value.

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