“isEqualToString” Cocoa错误 [英] "isEqualToString" Cocoa error

查看:122
本文介绍了“isEqualToString” Cocoa错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制台中收到错误讯息:


2009-05-30 20:17:05.801 ChuckFacts [1029:20b] *** - [Joke isEqualToString:]:unrecognized
选择器发送到实例0x52e2f0


这是我的代码错误来自:

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

if(cell == nil){
[[NSBundle mainBundle] loadNibNamed:@TableCellowner: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是一个充满笑话的数组,



此外,您看到的部分的错误说明:


发送到实例0x52e2f0


我如何确定什么是0x52e2f0,因此下次更容易找到问题?

解决方案


为什么会出现这个错误?


因为你发送 isEqualToString :到一个Joke对象,并且你的Joke对象不响应 isEqualToString:



你可能不是故意发送消息到你的笑话对象;



你说的是 jokes 是一个数量充满笑话。但是,在您的代码中,您可以这样做:


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


除了异常,我想



将一个Joke对象放入 NSString * 变量不会将Joke对象转换为NSString。你所做的就是告诉编译器该变量包含一个NSString,然后放入一个笑话。我称之为向编译器说谎。



修复此问题的第一步是删除谎言并在其位置恢复真相:

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

如果你这样做后立即编译,你会注意到编译器已经开始给你一个警告后面几行:


  jokeTextLabel.text = jokeText; 

警告:将不同Objective-C类型的'setText:'的参数1传递


这当然是对的。笑话仍然不是NSStrings。



当你向Joke对象请求它的文本时,实际的解决方案就出现了(我假​​设它有一个属性,并且该属性的值是一个NSString),并给 jokeTextLabel.text setter 。 / p>


我如何确定0x52e2f0是什么,所以下次更容易找到问题?


在Xcode的Breakpoints窗口中,在 objc_exception_throw 上设置断点。然后运行你的程序。当异常发生时,调试器将停止您的程序,并且调试器窗口将打开。然后,在调试器控制台中键入 po 0x52e2f0 。 ( po 代表打印对象。)



我假设它也适用于iPhone应用程序。


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

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

How can I determine what "0x52e2f0" is so it would be easier to find the problem next time?

解决方案

Why is that error coming in?

Because you're sending isEqualToString: to a Joke object, and your Joke objects don't respond to isEqualToString:.

You're probably not intentionally sending that message to your Joke objects; instead, you're passing or returning Joke objects to something that is expecting NSString objects.

You say that jokes is an "array of full of 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. All you're doing is telling the compiler that the variable contains an NSString, then putting a Joke into it instead. 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

It's right, of course. Jokes still aren't 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.

How can I determine what "0x52e2f0" is so it would be easier to find the problem next time?

In Xcode's Breakpoints window, set a breakpoint on 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 stands for "print object".)

This works for Mac apps; I'm assuming it'd also work for iPhone apps.

这篇关于“isEqualToString” Cocoa错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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