简体   繁体   中英

Why am I getting an isEqualToString error in this Cocoa code?

I keep getting this error:

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

What is it? I never even called "isEqualToString".

Here Is my 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

And here is joke.h

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

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

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

@end

And here is where joke is being used

#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

Thanks

Replace this line:

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

with these lines:

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

The stack trace will help you find exactly what is calling isEqualToString: . Unfortunately, it's not giving you any symbols, so you'll have to do a little digging.

In Objective-C methods, there are two hidden parameters which are always passed as the first two arguments: self , a pointer to the object to which the message is being sent, and _cmd , a pointer to a C string containing the name of the message being sent. Examining the _cmd arguments in the stack frames will help you debug the problem.

The first thing you want to do is set a breakpoint right before the exception is thrown. Open up the debugger console (Cmd+Shift+R) and add a breakpoint to the function on the top of the stack trace by typing:

break 2438463755

Now run your app, and the debugger should break right before throwing the exception. It should also give you a full symbolic backtrace; if not, you'll have to walk the stack yourself. You can walk the stack and print out the value of the various _cmd parameters.

You're shadowing a ivar name with the function argument. Try changing your jokeWithValue: method to this:

In joke.h:

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

in joke.m:

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

Notice that the NSString variable name has changed so it no longer shadows the iVar joke.

EDIT:

On seeing how joke is called, it looks as if you're assigning a joke object to cell.text, which I believe is expecting an NSString not a joke.

try setting:

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

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

The error is not in the definition of the Joke class, but somewhere it's being used. In most cases with errors like this, it's the result of a memory management error — some object (presumably a string) gets deallocated and another gets allocated in its old memory location. Try running with NSZombieEnabled and see if it turns up a message to a dealloced object.

您的错误消息没有显示,但我假设您遇到了一个语句,该语句在后台调用isEqualToString,而其中一个对象不是字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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