简体   繁体   English

“isEqualToString”Cocoa错误

[英]“isEqualToString” Cocoa error

I am getting an error in my console: 我在控制台中收到错误:

2009-05-30 20:17:05.801 ChuckFacts[1029:20b] *** -[Joke isEqualToString:]: unrecognized selector sent to instance 0x52e2f0 2009-05-30 20:17:05.801 ChuckFacts [1029:20b] *** - [Joke isEqualToString:]:无法识别的选择器发送到实例0x52e2f0

Here is my code that I believe the error is coming from: 这是我的代码,我相信错误来自:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Joke";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:nil]; 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                                   reuseIdentifier:CellIdentifier] autorelease];
    cell = tableCell;
}

NSString *jokeText = [jokes objectAtIndex:indexPath.row];
UILabel *jokeTextLabel = (UILabel*) [cell viewWithTag:1];
jokeTextLabel.text = jokeText; 
NSString *dateText = formattedDateString;
UILabel *dateTextLabel = (UILabel*) [cell viewWithTag:2];
dateTextLabel.text = dateText;
[self todaysDate];
return cell;
}

"jokes" is an array full of jokes, incase you needed to know “笑话”是一个充满笑话的阵列,你需要知道

Why is that error coming in? 为什么会出现这个错误?

Also, do you see the part of the error that says: 另外,您是否看到错误的部分内容:

sent to instance 0x52e2f0 发送到实例0x52e2f0

How can I determine what "0x52e2f0" is so it would be easier to find the problem next time? 如何确定“0x52e2f0”是什么,以便下次更容易找到问题?

Why is that error coming in? 为什么会出现这个错误?

Because you're sending isEqualToString: to a Joke object, and your Joke objects don't respond to isEqualToString: . 因为您正在向一个Joke对象发送isEqualToString:并且您的Joke对象不响应isEqualToString: .

You're probably not intentionally sending that message to your Joke objects; 您可能没有故意将该消息发送给您的Joke对象; instead, you're passing or returning Joke objects to something that is expecting NSString objects. 相反,您将Joke对象传递或返回到期望NSString对象的东西。

You say that jokes is an “array of full of jokes”. 你说jokes是一个“充满笑话的阵列”。 Yet, in your code, you do this: 但是,在您的代码中,您执行此操作:

 NSString *jokeText = [jokes objectAtIndex:indexPath.row]; UILabel *jokeTextLabel = (UILabel*) [cell viewWithTag:1]; jokeTextLabel.text = jokeText; 

Going by the exception, I'm guessing that by “array full of jokes”, you meant “array of Joke objects”. 除了异常之外,我猜测通过“数组充满笑话”,你的意思是“一堆笑话对象”。

Putting a Joke object into an NSString * variable does not turn the Joke object into an NSString. 将Joke对象放入NSString *变量不会将Joke对象转换为NSString。 All you're doing is telling the compiler that the variable contains an NSString, then putting a Joke into it instead. 您所做的只是告诉编译器该变量包含一个NSString,然后将一个Joke放入其中。 I call this “lying to the compiler”. 我称之为“对编译器撒谎”。

The first step in fixing this is to remove the lie and restore truth in its place: 解决这个问题的第一步是取消谎言并在其位置恢复真相:

Joke *joke = [jokes objectAtIndex:indexPath.row];

If you compile immediately after doing this, you'll notice that the compiler has started giving you a warning a couple of lines later: 如果在执行此操作后立即编译,您会注意到编译器已经开始在几行之后给出警告:

 jokeTextLabel.text = jokeText; 

warning: passing argument 1 of 'setText:' from distinct Objective-C type 警告:从不同的Objective-C类型传递'setText:'的参数1

It's right, of course. 当然,这是对的。 Jokes still aren't NSStrings. 笑话仍然不是NSStrings。 Now that you're honest about the variable's type, the compiler can catch this for you. 现在你对变量的类型很诚实,编译器可以为你捕获这个。

The actual fix comes when you ask the Joke object for its text (I assume that it has a property for this, and that the value of that property is an NSString) and give that to the jokeTextLabel.text setter. 当你问其文本的笑话对象(我认为它会为这个属性,以及该属性的值是一个NSString), 给予到实际修复来jokeTextLabel.text制定者。

How can I determine what "0x52e2f0" is so it would be easier to find the problem next time? 如何确定“0x52e2f0”是什么,以便下次更容易找到问题?

In Xcode's Breakpoints window, set a breakpoint on objc_exception_throw . 在Xcode的Breakpoints窗口中,在objc_exception_throw上设置断点。 Then run your program. 然后运行你的程序。 When the exception happens, the debugger will stop your program, and the Debugger window will open. 发生异常时,调试器将停止程序,调试器窗口将打开。 Then, type po 0x52e2f0 into the Debugger Console. 然后,在调试器控制台中键入po 0x52e2f0 ( po stands for “print object”.) po代表“打印对象”。)

This works for Mac apps; 这适用于Mac应用程序; I'm assuming it'd also work for iPhone apps. 我认为它也适用于iPhone应用程序。

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. 您不能将Joke对象分配给UILabel的text属性 - 它只需要NSString

(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 NSString s explicitly when it wants a string.) (Cocoa不像Java或C ++,任何对象都可以通过一些.toString()方法自动强制转换为字符串。通常,框架在需要字符串时会明确要求NSString 。)

Here's what's happening: you're assigning a Joke object to the text property. 这是正在发生的事情:您正在为text属性分配一个Joke对象。 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). Cocoa允许你快速和松散地使用这样的类型,在这种情况下甚至没有警告,因为它被隐含地理解为NSString( id类型将默默地成为你分配给它的任何类型)。 But when it tries to call isEqualToString: (an NSString method) on a Joke object, of course it fails. 但是当它试图在Joke对象上调用isEqualToString:一个NSString方法)时,它当然会失败。

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. 至于如何识别对象:你可以在调试器中发出po 0x52e2f0命令,如果内存没有被完全打开,这通常有效。 It'll print an Objective-C representation of the object at that address. 它将在该地址打印对象的Objective-C表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM