Unrecognized selector error for isEqualToString: when setting text of a label

这一生的挚爱 提交于 2019-12-02 10:49:58
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9028390

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).

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!