为什么我在此Cocoa代码中收到isEqualToString错误?

时间:2009-05-14 20:21:40

标签: objective-c cocoa

我一直收到这个错误:

alt text http://img514.imageshack.us/img514/2203/help.tif

这是什么?我甚至都没有打电话给“isEqualToString”。

这是我的Joke.M

@implementation Joke
@synthesize joke;
@synthesize rating;


- (id)init {
[super init];
return self;
 }

- (void)dealloc {
[joke release];
[super dealloc];    
}

+ (id)jokeWithValue:(NSString *)joke {
Joke *j = [[Joke alloc] init];
j.joke = joke;
return [j autorelease];
}

@end

这是joke.h

@interface Joke : NSObject {
NSString *joke;
int rating;
}

+ (id)jokeWithValue:(NSString *)joke;

@property (readwrite, copy) NSString *joke;
@property (readwrite) int rating;

@end

这就是玩笑的地方

#import "TableViewController.h"
#import "Joke.h"

@implementation TableViewController
@synthesize jokes;

- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
    self.jokes = [NSMutableArray arrayWithObjects:
                  [Joke jokeWithValue:@"If you have five dollars and Chuck Norris has five dollars, Chuck Norris has more money than you"],
                  [Joke jokeWithValue:@"There is no 'ctrl' button on Chuck Norris's computer. Chuck Norris is always in control."],
                  [Joke jokeWithValue:@"Apple pays Chuck Norris 99 cents every time he listens to a song."],
                  [Joke jokeWithValue:@"Chuck Norris can sneeze with his eyes open."],
                  nil];
    }
return self;
 }

- (void)viewDidLoad {
self.navigationItem.leftBarButtonItem = self.editButtonItem;
} 


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Saying how many sections wanted (Just like in address, where sorts by first name)
return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section {
return [jokes count];
 }


 - (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Team";    
  UITableViewCell *cell = 
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
             initWithFrame:CGRectZero 
             reuseIdentifier:CellIdentifier] autorelease];
   }
cell.text = [jokes objectAtIndex:indexPath.row];
   return cell;
  }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath {
 }


 - (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
 }

 - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:
 (UIInterfaceOrientation)interfaceOrientation {
  return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  }

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; 
 }

 - (void)dealloc {
 [jokes release];
 [super dealloc];
 }

 @end

由于

5 个答案:

答案 0 :(得分:11)

替换此行:

cell.text = [jokes objectAtIndex:indexPath.row];

这些行:

Joke *j = (Joke *)[jokes objectAtIndex:indexPath.row];
if( j )
  cell.text = j.joke;

答案 1 :(得分:3)

堆栈跟踪将帮助您准确找到调用isEqualToString:的内容。不幸的是,它没有给你任何符号,所以你必须做一些挖掘。

在Objective-C方法中,有两个隐藏参数,它们总是作为前两个参数传递:self,指向发送消息的对象的指针,以及_cmd,指向包含要发送的消息名称的C字符串的指针。检查堆栈帧中的_cmd参数将帮助您调试问题。

您要做的第一件事就是在抛出异常之前设置断点。打开调试器控制台(Cmd + Shift + R)并通过键入以下内容向堆栈跟踪顶部的函数添加断点:

break 2438463755

现在运行你的应用程序,调试器应该在抛出异常之前中断。它还应该给你一个完整的象征性回溯;如果没有,你将不得不自己走路。您可以遍历堆栈并打印出各种_cmd参数的值。

答案 2 :(得分:2)

您正在使用函数参数遮蔽ivar名称。尝试将jokeWithValue:方法更改为:

在joke.h中:

+ (id)jokeWithValue:(NSString *)aJoke;

在joke.m中:

+ (id)jokeWithValue:(NSString *)aJoke {
     Joke *j = [[Joke alloc] init];
     j.joke = aJoke;
     return [j autorelease];
}

请注意,NSString变量名已更改,因此不再隐藏iVar笑话。

修改

在看到如何调用笑话时,看起来好像你正在将一个笑话对象分配给cell.text,我认为这个期望NSString不是一个笑话。

尝试设置:

cell.text = [[jokes objectAtIndex:index] joke];

in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

答案 3 :(得分:1)

错误不在Joke类的定义中,而是在某个地方使用它。在大多数情况下会出现这样的错误,这是内存管理错误的结果 - 某个对象(可能是一个字符串)被释放,另一个被分配到旧的内存位置。尝试使用NSZombieEnabled运行,看看它是否向已解除分配的对象发送消息。

答案 4 :(得分:0)

你的错误信息没有显示但是我假设你遇到了一个声明,后面是调用isEqualToString并且其中一个对象不是字符串