Ios 设置标签文本时,isEqualToString的选择器错误无法识别

Ios 设置标签文本时,isEqualToString的选择器错误无法识别,ios,objective-c,cocoa-touch,nsstring,unrecognized-selector,Ios,Objective C,Cocoa Touch,Nsstring,Unrecognized Selector,我试图从模型对象的一个属性(searchRecipeDetailsVariable)设置标签文本,但出现错误 //Extract number of servings from dictionary and place in model self.searchedRecipeDetailsVariable.numberOfServings = [self.detailedSearchYummlyRecipeResults objectForKey: @"numberOfServings"]; /

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

//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时,我收到错误:

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

你可以想象,我不太清楚为什么。我尝试过用字符串直接设置文本,如下所示,效果很好

self.numberOfServingsLabel.text = @"500";
然后为了测试,我确实有一个字符串,我尝试了下面的方法。这个很好用

NSString *test = self.searchedRecipeDetailsVariable.numberOfServings;
NSLog(@"test numberof servings string, %@", test);
当我将鼠标悬停在测试上时,我打印了描述。我不知道它是否有用,但它是:

打印测试说明:2

当我将鼠标悬停在它上面时,它确实会说它是一个
NSString*
,最后它有
(int)2
。不知道那意味着什么

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9028390
与其他情况一样,错误消息是一个有意义的英语句子,描述问题。它告诉您,
self.searchedRecipedTailsVariable.numberOfServings
是一个
NSNumber
。无论您将其声明为
NSString
,因为Objective-C是动态类型的(对象的编译时类型声明只是为了向编译器提供提示,它可能与实际情况无关)

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

NSString *test = [NSString stringWithFormat:@"%d",
    self.searchedRecipeDetailsVariable.numberOfServings.intValue];
与其他情况一样,错误消息是一个有意义的英语句子,描述问题。它告诉您,
self.searchedRecipedTailsVariable.numberOfServings
是一个
NSNumber
。无论您将其声明为
NSString
,因为Objective-C是动态类型的(对象的编译时类型声明只是为了向编译器提供提示,它可能与实际情况无关)

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

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

谢谢我已经试过了,效果不错。我想我需要读一读这本书。谢谢。我已经试过了,效果不错。我想我需要读一读这本书。