2011-08-09 115 views
0

我有這個代碼NSMutable陣列錯誤

SBJsonParser *vol= [[SBJsonParser alloc]init]; 
if (body) { 
    NSArray *feeds = [vol objectWithString:body error:nil]; 
    NSDictionary *results = [body JSONValue]; 
    NSArray *subs = [results valueForKey:@"subscriptions"]; 
    NSArray *list; 
    NSMutableArray *sub; 

    for (NSDictionary *iscrizione in subs){ 
     NSLog([iscrizione objectForKey:@"title"]); 
     NSLog([iscrizione objectForKey:@"htmlUrl"]); 
     list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil]; 
     [sub addObject:list]; 

    } 

但是當我嘗試添加列表中的NSMutableArray程序崩潰。爲什麼? 如何在數據結構中插入[iscrizione objectForKey:@"title"][iscrizione objectForKey:@"htmlUrl"]

編輯**

SBJsonParser *vol= [[SBJsonParser alloc]init]; 
if (body) { 
    NSArray *feeds = [vol objectWithString:body error:nil]; 
    NSDictionary *results = [body JSONValue]; 
    NSArray *subs = [results valueForKey:@"subscriptions"]; 
    NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]]; 
    for (NSDictionary *iscrizione in subs){ 

     [sub addObject:iscrizione]; 

    } 
    NSDictionary *it=[[NSDictionary alloc]initWithDictionary:[sub objectAtIndex:0]]; 
    NSLog([it objectForKey:@"title"]); 

} 

這是我的代碼。但我不明白爲什麼我不能NSLog [it objectForKey:@「title」]。

+0

請,提供了一種碰撞信息。它會拋出什麼錯誤? –

回答

2

你沒有初始化你的NSMutableArray。

應該

NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]];

從技術上講,你可以簡單地用 NSMutableArray *sub = [[NSMutableArray array]; 分配它很好,但,這可能是更有效的,因爲你已經知道有多少數據條目會出現。

只要注意內存管理,你需要在第一種情況下釋放它,在第二種情況下它是自動釋放的。

+0

感謝您的回答。我編輯我的代碼,但我有另一個問題!你能解釋這是怎麼發生的嗎? –

+0

NSLog至少需要一個參數,但第一個參數是格式對象。你需要輸入:'NSLog(@「My object:%@」,[dict objectForKey:@「key」]);'。另外,爲什麼你從第一個創建另一個NSDictionary,分配更多的內存? –

+0

對於測試...我想看看數組是否包含所有對象...以後使用它...我嘗試使用NSDictionary *它= [[NSDictionary alloc] initWithDictionary:[sub objectAtIndex:0]]; NSLog(@「My object:%@」,[it objectForKey:@「title」]);就像在你的建議,但我得到這個錯誤***終止應用程序由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:索引0超出空數組的界限' –

0

sub未分配內存。您可以在if條件之外創建一個空數組或在代碼中保留一個引用。

0

您應該先使用allocinit MutableArray,然後才能使用它。

SBJsonParser *vol= [[SBJsonParser alloc]init]; 
if (body) { 
    NSArray *feeds = [vol objectWithString:body error:nil]; 
    NSDictionary *results = [body JSONValue]; 
    NSArray *subs = [results valueForKey:@"subscriptions"]; 
    NSArray *list; 
    NSMutableArray *sub = [[NSMutableArray alloc] initWithCapacity:[subs count]]; 

    for (NSDictionary *iscrizione in subs){ 
     NSLog(@"title: %@" ,[iscrizione objectForKey:@"title"]); 
     NSLog(@"htmlUrl: %@", [iscrizione objectForKey:@"htmlUrl"]); 
     list=[NSArray arrayWithObjects:[iscrizione objectForKey:@"title"], [iscrizione objectForKey:@"htmlUrl"], nil]; 
     [sub addObject:list]; 

    } 
    // here you should add code to handle the sub array. 

    // Do not forget to release the allocated array 
    [sub release], sub =nil; 
} 
+0

感謝您的回答。我編輯我的代碼,但我有另一個問題!你能解釋這是怎麼發生的嗎? - –

+0

我已經修正了NSLog語句爲'Jacob Gorban'建議。 – rckoenes

0

下面會做的正常工作......

NSMutableArray *sub = [[NSMutableArray alloc] init]; 
    for (NSDictionary *iscrizione in subs) 
    { 
     NSString *title = [NSString stringWithFormat:@"%@", [iscrizione objectForKey:@"title"]]; 
     NSString *htmlURL = [NSString stringWithFormat:@"%@",[iscrizione objectForKey:@"htmlUrl"]]; 
     list=[NSArray arrayWithObjects:title, htmlURL, nil]; 
     [sub addObject:list mutableCopy]; 
    }