2009-05-14 14 views
0

私はこのエラーを取得しておいてください。このCocoaコードでisEqualToStringエラーが発生するのはなぜですか?

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 

であり、ここで

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

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

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

@end 

joke.hであり、ここで冗談を

#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 

おかげ

+4

一部のオブジェクトは文字列のように比較されています。あなたはより多くの情報を与え、いくつかのコードを表示する必要があります。これが起こっている場所のコードを表示してください。 –

+0

Kk私のコードをそこに追加しました。申し訳ありませんが前に追加する –

+0

どこに定義されているだけでなく、ジョークが使用されているコードを投稿できますか? – chaos

答えて

11

この行を置き換えますと

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

をこれらの行:

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

ああ、それは働く!本当にありがとう! –

+2

これは、cell.textがジョークオブジェクトではなく文字列を必要とするため、これが機能します。前に、cell.textのアクセサは、あなたが与えたオブジェクト(ジョーク)を既に持っていたオブジェクト(NSString)と比較していたので、その例外を見ていたのです。これを修正できる別の方法は、isEqualToStringの実装をJokeオブジェクトに追加し、その要求をあなたのジョークプロパティに転送することでした。 –

+0

が理にかなっています!本当にありがとう –

0

を使用されている場所でありますあなたのエラーメッセージはdispではありませんあなたは舞台裏でisEqualToStringを呼び出し、オブジェクトの1つが文字列ではないというステートメントを実行したと仮定しています

1

エラーはJokeクラスの定義にはありませんが、どこかにある中古。このようなエラーのほとんどは、メモリ管理エラーの結果です。オブジェクト(おそらく文字列)が割り当て解除され、別のオブジェクトが古いメモリ位置に割り当てられます。 NSZombieEnabledを実行して、解放されたオブジェクトへのメッセージを表示するかどうかを確認してください。

+0

これはここに当てはまらないことが判明しましたが、このようなケースではしばしば問題になるため、私はGoogleの目的のために残しています。 – Chuck

2

あなたは関数の引数でivar名をシャドーイングしています。あなたのjokeWithValueを変更してみてください。これに方法:joke.hで

:joke.mで

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

:NSStringの変数名は、それはもはや影を変更していないことを

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

お知らせiVarのジョーク。

EDIT:

ジョークが呼び出された方法を見に私はNSStringのない冗談を期待していると信じている、cell.textにジョークオブジェクトを代入しているかのように、それが見えます。

のtry設定:

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

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

+0

これが問題になると私は驚くでしょう。彼はその方法では象牙を使用しないので、それを陰にすることは安全ではない(厳密には良い習慣ではないにしても)。 – Chuck

+0

これは実際にはどちらも実際にはNSStringであるため、実際に彼の例外を引き起こすことはありません。彼はたぶんJokeオブジェクトをどこかの文字列値のジョークと比較しています。 –

+0

まだ動作しません。同じエラー –

3

スタックトレースは、isEqualToString:を正確に見つけるのに役立ちます。残念なことに、それはあなたにシンボルを与えていないので、少し掘り下げなければなりません。

Objective-Cのメソッドでは、メッセージが送信されるオブジェクトへのポインタであるselfと、メッセージが送信されるオブジェクトへのポインタである_cmdの2つの隠しパラメータが常に含まれています。送信されるメッセージの名前。スタックフレーム内の_cmd引数を調べると、問題のデバッグに役立ちます。

まず、例外がスローされる直前にブレークポイントを設定します。デバッガコンソール(CMD + Shiftキー+ R)を開き、次のように入力して、スタックトレースの上の関数にブレークポイントを追加します。

break 2438463755 

今すぐあなたのアプリを実行すると、デバッガは投げる前に右破る必要があります例外。また、完全な象徴的なバックトレースを与える必要があります。もしそうでなければ、スタックを自分で歩かなければならないでしょう。スタックを歩いてさまざまなパラメータの値を表示することができます。

関連する問題