“isEqualToString” Cocoa error

后端 未结 2 680
暗喜
暗喜 2021-01-14 07:21

I am getting an error in my console:

2009-05-30 20:17:05.801 ChuckFacts[1029:20b] *** -[Joke isEqualToString:]: unrecognized selector sent to insta

2条回答
  •  广开言路
    2021-01-14 07:34

    By "an array full of jokes", you apparently mean "an array full of classes of type Joke". You can't assign a Joke object to a UILabel's text property—it takes NSString only.

    (Cocoa isn't like Java or C++, where any object can be coerced to a string automatically through some .toString() method. Generally the framework asks for NSStrings explicitly when it wants a string.)

    Here's what's happening: you're assigning a Joke object to the text property. Cocoa allows you to play fast and loose with types like this, without even a warning in this case since it's implicitly understood to be an NSString (the id type will silently become whatever type you're assigning it to). But when it tries to call isEqualToString: (an NSString method) on a Joke object, of course it fails.

    You need to assign the joke's text to the label instead.

    As for how to identify the object: you can issue the po 0x52e2f0 command in the debugger, which usually works if memory isn't completely borked. It'll print an Objective-C representation of the object at that address.

提交回复
热议问题