我有这个NSArray:
{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 :(得分:2)
<强>更新强>
我现在看到你想要一个
Animal
类型的对象而不是NSDictionary
。代码行animal[@"name"]
需要成为获取名称的访问器方法。我更新了我的代码,猜测你需要animal.name
。
你想要一个完整的索引(包含所有值的词典&#39; A&#39; - &#39; Z&#39;即使大多数键都有一个空数组)?
- (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]];
Animal *
中的故事animalArray
,但您的错误消息似乎表明NSMutuableArray *
对象存储在animalArray
中。
答案 1 :(得分: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]);
希望它可以帮助你......!
答案 2 :(得分: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 ..等。
答案 3 :(得分:0)
使用词典并设置对象
NSMutableArray *arr;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:arr forKey:@"L"];