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

0 cocoa objective-c

我一直收到这个错误:

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

它是什么?我甚至从未调用过"isEqualToString".

这是我的笑话.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
Run Code Online (Sandbox Code Playgroud)

这是joke.h

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

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

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

@end
Run Code Online (Sandbox Code Playgroud)

这里是玩笑的地方

#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
Run Code Online (Sandbox Code Playgroud)

谢谢

Jas*_*oco 11

替换此行:

cell.text = [jokes objectAtIndex:indexPath.row];
Run Code Online (Sandbox Code Playgroud)

用这些线:

Joke *j = (Joke *)[jokes objectAtIndex:indexPath.row];
if( j )
  cell.text = j.joke;
Run Code Online (Sandbox Code Playgroud)

  • 是的,这是有效的,因为cell.text需要一个字符串,而不是一个笑话对象.以前,cell.text的访问者将你给它的对象(一个笑话)与它已经拥有的对象(一个NSString)进行比较,这就是你看到异常的原因.解决这个问题的另一种方法是在你的Joke对象上添加一个isEqualToString:的实现,并将该请求转发给你的笑话属性. (2认同)