繁体   English   中英

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

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

我不断收到此错误:

替代文字http://img514.imageshack.us/img514/2203/help.tif

它是什么? 我什至从未称呼“ isEqualToString”。

这是我的笑话

@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

这是笑话

@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

谢谢

替换此行:

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

这些行:

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

堆栈跟踪将帮助您确切地找到isEqualToString: 不幸的是,它没有给您任何符号,因此您必须进行一些挖掘。

在Objective-C方法中,有两个隐藏参数始终作为前两个参数传递: self (指向将消息发送到的对象的指针)和_cmd (指向包含字符串名称的C字符串的指针)消息正在发送。 检查堆栈帧中的_cmd参数将帮助您调试问题。

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

break 2438463755

现在运行您的应用程序,调试器应在引发异常之前立即中断。 它还应该给您完整的符号回溯。 如果没有,您将不得不自己走栈。 您可以遍历堆栈并打印出各种_cmd参数的值。

您正在使用function参数隐藏ivar名称。 尝试将jokeWithValue:方法更改为此:

在joke.h中:

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

在笑话中:

+ (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];

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

错误不在Joke类的定义中,而是在使用它的地方。 在大多数情况下,由于类似这样的错误,这是内存管理错误的结果-一些对象(可能是字符串)被释放,而另一个对象则在其旧的内存位置被分配。 尝试与NSZombieEnabled一起运行,看看它是否将一条消息发送给已分配的对象。

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

暂无
暂无

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

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