isEqualToString无法识别的选择器错误:设置标签文本时 [英] Unrecognized selector error for isEqualToString: when setting text of a label

查看:145
本文介绍了isEqualToString无法识别的选择器错误:设置标签文本时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的模型对象(searchRecipeDetailsVariable)的属性之一设置标签的文本,但出现错误

I am trying to set the text of a label from one of the properties of my model object (searchRecipeDetailsVariable), but I get an error

//Extract number of servings from dictionary and place in model
self.searchedRecipeDetailsVariable.numberOfServings = [self.detailedSearchYummlyRecipeResults objectForKey: @"numberOfServings"];
//log number of servings to check that it works
NSLog(@"Number of Servings, %@",self.searchedRecipeDetailsVariable.numberOfServings);
self.numberOfServingsLabel.text = self.searchedRecipeDetailsVariable.numberOfServings;

当我打印该值时,我可以正确看到该数字.但是,当我尝试设置numberOfServingsLabel.text时,我收到错误消息:

When I print the value, I can see the number correctly. However, when I attempt to set numberOfServingsLabel.text I receive the error:

-[__ NSCFNumber isEqualToString:]:无法识别的选择器已发送到实例0x9028390

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9028390

您可以想象,我不太清楚为什么.我尝试过直接使用字符串设置文本,如下所示,这可行.

As you can imagine, I am not too sure why. I have tried setting the text directly with a string, like below and this works.

self.numberOfServingsLabel.text = @"500";

然后进行测试,实际上我确实有一个字符串,我在下面尝试过.效果很好.

Then to test I actually did have a string I tried the below. This works fine.

NSString *test = self.searchedRecipeDetailsVariable.numberOfServings;
NSLog(@"test numberof servings string, %@", test);

当我将鼠标悬停在test上时,我打印了说明.我不知道它是否会有用,但它是:

When I hover over test, I printed the description. I don't know if it will be useful but it was:

打印测试说明:2

Printing description of test: 2

当我将鼠标悬停在它上面时,它的确表示它是NSString *,最后它是(int)2.不知道那是什么意思.

When I hover over it, it does say it is an NSString * and at the end it has (int)2. Not to sure what that means.

推荐答案

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9028390

就像在其他情况下一样,错误消息是描述问题的有意义的英文句子.它告诉您self.searchedRecipeDetailsVariable.numberOfServingsNSNumber.无论您将其声明为NSString,还是因为Objective-C都是动态类型的(对象的编译时类型声明仅用于向编译器提供提示,所以可能与现实无关).

Just like in any other case, the error message is a meaningful English sentence describing the problem. It tells you that self.searchedRecipeDetailsVariable.numberOfServings is an NSNumber. No matter you declared it as an NSString, since Objective-C is dynamically typed (the compile-time type declaration for an object is just for giving hints to the compiler, it may have nothing to do with reality).

您需要将其转换为字符串,或者使用NSNumberFormatter(正确的方式),或者获取其描述(不建议使用,永远不要依赖该描述),等等.例如:

You need to convert it to a string, perhaps using NSNumberFormatter (the proper way), or getting its description (that's not recommended, the description is never to be relied upon), etc. For example:

NSString *test = [NSString stringWithFormat:@"%d",
    self.searchedRecipeDetailsVariable.numberOfServings.intValue];

这篇关于isEqualToString无法识别的选择器错误:设置标签文本时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆