2014-09-10 58 views
-1

我有这样的NSArray:转换的NSArray到NSDictionary中的切片

{name="LOBSTER",photo="prawn.jpg"},{name="COW", photo"cow_grass.png"},...so on 

我想创建在UITableView的部分,所以我需要一个像这样的词典:

{L=(name="Lobster";photo="prawn.jpg"),C=(name="COW"; photo="cow_grass.png"),...so on} 

能否请您帮助我告诉我该怎么做?谢谢。

这是我怎么加我的数组:

animalsArray = [[NSMutableArray alloc] init]; 
     //Loop through our JsonDict 
     long countArray = [[dataDict valueForKey:@"animal"]count]; 
     for (int i = 0; i < countArray ; i++) 
     { 
     NSString * cName = [[[dataDict valueForKey:@"animal"]objectAtIndex:i]valueForKey: @"name"]; 
     NSString * cPhoto = [[[dataDict valueForKey:@"animal"]objectAtIndex:i]valueForKey: @"photo"]; 

     [animalArray addObject:[[Animals alloc]initWithName:cName andPhoto:cPhoto]]; 
+0

你想要的是一个字典数组字典。你上面显示的是一本我不知道是什么的字典。 – 2014-09-10 12:19:10

+0

@ edward..Refer我更新的答案.. 你会得到部分数..使用我的代码.. – Vidhyanand 2014-09-10 12:34:58

回答

2

UPDATE

I now see that you want an object of type Animal instead of an NSDictionary . The line of code which was animal[@"name"] needs to be what ever accessor method to get the name. I updated my code to guess that you need animal.name .

你想要一个完整的指数(所有的值 'A' 的字典 - 'Z' 即使大多数键有一个空数组)?

- (NSDictionary *)fullIndexOfAnimalsFromAnimals:(NSArray *)animals 
{ 
    NSMutableDictionary *result = [NSMutableDictionary dictionary]; 

    // Create a key for each letter of the alphabet. 
    for (char ch = 'A'; ch <= 'Z'; ++ch) { 
     NSString *key = [NSString stringWithFormat:@"%c", ch]; 
     result[key] = [NSArray array]; 
    } 

    // Add all the animals keyed by the first character of their name. 
    for (Animal *animal in animals) { 
     NSString *key = [animal.name substringToIndex:1] uppercaseString]; 
     result[key] = [result[key] arrayByAddingObject:animal]; 
    } 

    return [result copy]; // NSMutableDictionary * to NSDictionary * 
} 

你想要一个部分索引(一个字典只有键有值)吗?

- (NSDictionary *)partialIndexOfAnimalsFromAnimals:(NSArray *)animals 
{ 
    NSMutableDictionary *result = [NSMutableDictionary dictionary]; 

    // Add all the animals keyed by the first character of their name. 
    for (Animal *animal in animals) { 
     NSString *key = [animal.name substringToIndex:1] uppercaseString]; 
     NSArray *value = result[key]; 
     if (value == nil) { 
      result[key] = [NSArray arrayWithObject:animal]; // Create new array 
     } else { 
      result[key] = [value arrayByAddingObject:animal]; // Add to existing 
     } 
    } 

    return [result copy]; // NSMutableDictionary * to NSDictionary * 
} 

作为第二个更新,有什么奇怪的。

[animalArray addObject:[[Animals alloc] initWithUser_id:cUser_id 
               andName:cName 
               andNik:cNik 
              andJob_title:cJob_title 
               andPhone:cPhone 
              andRoom_no:cRoom_no 
               andPhoto:cPhoto]]; 

animalArray storying Animal *,但你有错误信息似乎表明,NSMutuableArray *对象存储在animalArray

+0

我得到一个错误说,预期的方法来读取字典元素找不到'NSMutableArray *'类型的对象'我该怎么办?@JefferyThomas – Edward 2014-09-10 11:28:52

+0

@Edward你在哪里得到这个错误,没有一个示例代码使用NSMutableArray。 – 2014-09-10 12:25:16

+0

动物[@“name”] @JefferyThomas – Edward 2014-09-10 12:27:59

0

拿字典,并设置对象的关键

低于
NSMutableArray *arr; 
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; 
[dict setObject:arr forKey:@"L"]; 
1

希望是你正在寻找的代码。

NSArray *currentArray = ... 

// New dictionary object to hold the grouped results. 
NSMutableDictionary *dictionaryWithSections = [NSMutableDictionary dictionary]; 

for (NSDictionary *dictionary in currentArray) { 
    NSString *section = [[[dictionary objectForKey:@"name"] substringToIndex:1] uppercaseString]; 

    // Check whether section already exits. 
    if ([dictionaryWithSections objectForKey:section]) { 
     [[dictionaryWithSections objectForKey:section] addObject:dictionary]; 
    } else { 
     NSMutableArray *sectionData = [NSMutableArray arrayWithObject:dictionary]; 
     [dictionaryWithSections setObject:sectionData forKey:section]; 
    } 
} 

dictionaryWithSections是您用分组获得的新字典。 [dictionaryWithSections allKeys]将返回所需的所有部分标题。即L,C ..等

+0

我得到错误说 - >终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [Animals objectForKey :]:无法识别的选择器发送到实例0x11195b0e0' – Edward 2014-09-10 11:34:20

+0

你正在尝试的代码是什么?来自这个答案还是其他答案? – uiroshan 2014-09-10 11:40:54

+0

我正在尝试你的代码 – Edward 2014-09-10 11:42:41

2

试试下面代码..

NSMutableDictionary *finalResultDict=[NSMutableDictionary dictionary]; 

    for(NSDictionary *wildanimals in animalsarray) 
    { 

    NSString *key=[[wildanimals[@"name"] substringToIndex:1] uppercaseString]; 

    if([wildanimals[@"name"] hasPrefix:key]) 
    { 
     [finalResultDict setValue: wildanimals forKey:key] ; 
    } 
    } 

您可以通过设置allKeysArray区间的数目计算如下

NSArray * allKeysArray = [finalResultDict allKeys]; 
NSLog(@"Count : %d", [finalResultDict count]); 

希望它可以帮助你......!

+0

if函数表示的错误 - >文字和标识符之外不允许使用非ASCII字符。并建议我删除hasPrefix中的'ha'@ Vidhyanand900 – Edward 2014-09-10 11:54:23

+0

我修改了我的答案..检查它.. – Vidhyanand 2014-09-10 12:03:25

+0

它清除了以前的错误,但现在它无法识别动物的setValue。我需要在那里放置什么? @ Vidhyanand900 – Edward 2014-09-10 12:08:19