2013-12-18 56 views
-1

我的小問題是。創建一個表中的關鍵值在objective-c

我得到的數據和我過濾jusqu'la一切還好。

,我想結果見表mettres期望的結果的 例如:

data:[ 
{ 
    id=1, 
    name="toto" 
}, 
{ 
    id=1, 
    name="toto" 
} 
] 

等.. 所以我已經嘗試做一個簡單的表,但它讓我就在去年的元素,我做不知道我犯了什麼錯誤。 這是我做過什麼

NSArray *array1; 
for (int i=0; i<[catg count]; i++) { 
if ([ catg[i][@"name"] compare:@"Expositions" options:NSCaseInsensitiveSearch] ==  NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Peinture/Illustration" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Photographie" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Spectacles" options:NSCaseInsensitiveSearch] == NSOrderedSame) { 

    array1 = @[@"data",@{@"id": catg[i][@"id"],@"name": catg[i][@"name"]}]; 
    } 
} 

THKS

回答

0

你在你的循環

NSMutableArray *array1 = [NSMutableArray array]; 
for (int i=0; i<[catg count]; i++) { 
if ([ catg[i][@"name"] compare:@"Expositions" options:NSCaseInsensitiveSearch] ==  NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Peinture/Illustration" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Photographie" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Spectacles" options:NSCaseInsensitiveSearch] == NSOrderedSame) { 

    NSArray *tempArray = @[@"data",@{@"id": catg[i][@"id"],@"name": catg[i][@"name"]}]; 
    [array1 addObject:tempArray]; 
    } 
} 

應該解決您的問題的每一次迭代覆蓋陣列1。

+0

我覺得你的代碼,你將不得不在 '陣列1' 類似的結尾:[{@ 「數據」,...},{@ 「數據」,...}] –

0

對於每個循環你設置你的數組。 試圖修改這樣的代碼:

NSMutableArray *array1 = [[NSMutableArray allo] init]; 
for (int i=0; i<[catg count]; i++) { 
if ([ catg[i][@"name"] compare:@"Expositions" options:NSCaseInsensitiveSearch] ==  NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Peinture/Illustration" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Photographie" options:NSCaseInsensitiveSearch] == NSOrderedSame || 
    [ catg[i][@"name"] compare:@"Spectacles" options:NSCaseInsensitiveSearch] == NSOrderedSame) { 

    [array1 addObject:@[@{@"id": catg[i][@"id"],@"name": catg[i][@"name"]}]]; 
    } 
} 

NSDictionary *result = @{@"data": array1}; 
NSLog(@"%@", result); 

PS:很高興谷歌翻譯用法語單詞:對

0

是,

,但它讓我一個這樣的事

data, 
      { 
     id = 1; 
     name ="toto"; 
    } 
), 
    (
    data, 
      { 
     id = 8; 
     name = "titi"; 
    } 
) 

我想要

data:[ 
      { 
     id = 1; 
     name ="toto"; 
    }, 
    data, 
      { 
     id = 8; 
     name = "titi"; 
    } 
] 

這將是另一種方式來做到這一點?

THKS

相關問題