2010-12-27 10 views
4

NSDictionaryをTableViewControllerに送信しようとしていますが、元々は.plistファイルからデータが取得されています。私は単に階層の下にあるオブジェクトを新しいTableViewControllerに送信したいだけです。しかし、numberOfSectionsInTableViewの項目数を数えようとすると問題が発生します。NSDictionaryからすべてのキーをカウントするとEXC_BAD_ACCESSが返されます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Gets the dictionary for the currently selected row 
NSDictionary *dict = [[data objectForKey:[[data allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; 

// Checks if the key "Room" exists 
if([dict objectForKey:@"Room"]) { 
    SalesFairSessionTableViewController *sessionViewController = [[SalesFairSessionTableViewController alloc] init]; 

    // Sets the data in the subview Controller 
    [sessionViewController setData:[dict objectForKey:@"Room"]]; 

    // And the title 
    [sessionViewController setTitle:[dict objectForKey:@"Title"]]; 

    // Problem is here... returns EXC_BAD_ACCESS 
    NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

    [self.navigationController pushViewController:sessionViewController animated:YES]; 
    [sessionViewController release]; 
} 
} 

私はちょうどこのようallKeysを使用する場合:

NSLog([[dict objectForKey:@"Room"] allKeys]); 

それはコンソールに( "項目1"、 "アイテム2")を返します。

しかし、私はこのような「カウント」メソッドを追加する場合:

NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

私はちょうど得る:「EXC_BAD_ACCESS」:プログラムが信号を受信。

私はここで何が欠けていますか?

答えて

10
NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

カウントはintを与えていますが、オブジェクトへのポインタを期待し、%@で印刷。代わりに%dを使用してください。

+0

おかげで、私がこのような愚かな間違い修正するには、この行を変更します。問題が解決しました :) – kroofy

3

現在のところ、コードは、NSLog文字列にオブジェクトが必要であると伝えています。 countNSIntegerを返します。したがって、エラーです。これに

NSLog(@"%@", [[[dict objectForKey:@"Room"] allKeys] count]); 

NSLog(@"%i", [[[dict objectForKey:@"Room"] allKeys] count]); 
関連する問題