3 iphone cocoa exception objective-c
我在控制台中收到错误:
2009-05-30 20:17:05.801 ChuckFacts [1029:20b]*** - [Joke isEqualToString:]:无法识别的选择器发送到实例0x52e2f0
这是我的代码,我相信错误来自:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
"笑话"是一个充满笑话的阵列,你需要知道
为什么会出现这个错误?
另外,您是否看到错误的部分内容:
发送到实例0x52e2f0
如何确定"0x52e2f0"是什么,以便下次更容易找到问题?
Pet*_*sey 19
为什么会出现这个错误?
因为您正在发送isEqualToString:一个Joke对象,并且您的Joke对象没有响应isEqualToString:.
您可能没有故意将该消息发送给您的Joke对象; 相反,您将Joke对象传递或返回到期望NSString对象的东西.
你说这jokes是一个"充满笑话的阵列".但是,在您的代码中,您执行此操作:
Run Code Online (Sandbox Code Playgroud)NSString *jokeText = [jokes objectAtIndex:indexPath.row]; UILabel *jokeTextLabel = (UILabel*) [cell viewWithTag:1]; jokeTextLabel.text = jokeText;
除了异常之外,我猜测通过"充满笑话的数组",你的意思是"笑话对象的数组".
将Joke对象放入NSString *变量不会将Joke对象转换为NSString.您所做的只是告诉编译器该变量包含一个NSString,然后将一个Joke放入其中.我称之为"对编译器撒谎".
解决这个问题的第一步是取消谎言并在其位置恢复真相:
Joke *joke = [jokes objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)
如果在执行此操作后立即编译,您会注意到编译器已经开始在几行之后给出警告:
Run Code Online (Sandbox Code Playgroud)jokeTextLabel.text = jokeText;警告:从不同的Objective-C类型传递'setText:'的参数1
当然,这是对的.笑话仍然不是NSStrings.现在你对变量的类型很诚实,编译器可以为你捕获这个.
当你问其文本的笑话对象(我认为它会为这个属性,以及该属性的值是一个NSString),并给予实际的修复来这的jokeTextLabel.text制定者.
如何确定"0x52e2f0"是什么,以便下次更容易找到问题?
在Xcode的Breakpoints窗口中,设置断点objc_exception_throw.然后运行你的程序.发生异常时,调试器将停止程序,调试器窗口将打开.然后,键入po 0x52e2f0调试器控制台.(po代表"打印对象".)
这适用于Mac应用程序; 我认为它也适用于iPhone应用程序.
| 归档时间: |
|
| 查看次数: |
7314 次 |
| 最近记录: |