[英]Convert NSArray to NSDictionary for sectioning
I have this NSArray: 我有这个NSArray:
{name="LOBSTER",photo="prawn.jpg"},{name="COW", photo"cow_grass.png"},...so on
And I want to create section in the UITableView, so I need a dictionary like this: 而且我想在UITableView中创建部分,所以我需要像这样的字典:
{L=(name="Lobster";photo="prawn.jpg"),C=(name="COW"; photo="cow_grass.png"),...so on}
Could you please help me tell me how to do it? 您能帮我告诉我怎么做吗? Thank you. 谢谢。
This is how I add my array: 这是我添加数组的方式:
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]];
UPDATE 更新
I now see that you want an object of type
Animal
instead of anNSDictionary
. 现在,我看到您想要的是Animal
类型的对象,而不是NSDictionary
类型的对象。 The line of code which wasanimal[@"name"]
needs to be what ever accessor method to get the name. 这行代码是animal[@"name"]
需要使用任何访问方法来获取该名称。 I updated my code to guess that you needanimal.name
. 我更新了代码,以为您需要animal.name
。
Do you want a full index (A dictionary with all the values 'A'–'Z' even if most keys have an empty array)? 您是否要一个完整的索引(即使大多数键都有一个空数组,字典也要具有所有值“ 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 *
}
Do you want a partial index (A dictionary with only the keys that have values)? 您是否需要部分索引(仅包含具有值的键的字典)?
- (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 *
}
As a second update, there is something odd. 作为第二次更新,有些奇怪。
[animalArray addObject:[[Animals alloc] initWithUser_id:cUser_id
andName:cName
andNik:cNik
andJob_title:cJob_title
andPhone:cPhone
andRoom_no:cRoom_no
andPhoto:cPhoto]];
Is storying Animal *
in animalArray
, but the error message you have seems to indicate that NSMutuableArray *
objects are being stored in animalArray
. 正在讲故事Animal *
在animalArray
,但是出现的错误消息似乎表明NSMutuableArray *
对象存储在animalArray
。
Try below code.. 尝试下面的代码。
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] ;
}
}
You can set number of sections by allKeysArray count as below 您可以通过allKeysArray计数来设置节数,如下所示
NSArray * allKeysArray = [finalResultDict allKeys];
NSLog(@"Count : %d", [finalResultDict count]);
Hope it helps you...! 希望它可以帮助您...!
Hope below is the code you are looking for. 希望下面是您正在寻找的代码。
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 is the new dictionary you get with grouping. dictionaryWithSections是通过分组获得的新词典。 [dictionaryWithSections allKeys]
will return all the section titles you want. [dictionaryWithSections allKeys]
将返回您想要的所有节标题。 ie L, C.. etc. 即L,C ..等
Take Dictionary and set object for key 拿字典并为键设置对象
NSMutableArray *arr;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:arr forKey:@"L"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.