简体   繁体   English

为什么在此可可代码中出现isEqualToString错误?

[英]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 替代文字http://img514.imageshack.us/img514/2203/help.tif

What is it? 它是什么? I never even called "isEqualToString". 我什至从未称呼“ 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: . 堆栈跟踪将帮助您确切地找到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. 在Objective-C方法中,有两个隐藏参数始终作为前两个参数传递: self (指向将消息发送到的对象的指针)和_cmd (指向包含字符串名称的C字符串的指针)消息正在发送。 Examining the _cmd arguments in the stack frames will help you debug the problem. 检查堆栈帧中的_cmd参数将帮助您调试问题。

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: 打开调试器控制台(Cmd + Shift + R)并通过输入以下内容在堆栈跟踪顶部的函数中添加断点:

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. 您可以遍历堆栈并打印出各种_cmd参数的值。

You're shadowing a ivar name with the function argument. 您正在使用function参数隐藏ivar名称。 Try changing your jokeWithValue: method to this: 尝试将jokeWithValue:方法更改为此:

In joke.h: 在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. 请注意,NSString变量名称已更改,因此不再笼罩iVar笑话。

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. 在了解笑话的调用方式后,看起来好像您是在为cell.text分配笑话对​​象,我相信这是一个NSString而不是笑话。

try setting: 尝试设置:

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

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

The error is not in the definition of the Joke class, but somewhere it's being used. 错误不在Joke类的定义中,而是在使用它的地方。 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. 尝试与NSZombieEnabled一起运行,看看它是否将一条消息发送给已分配的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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