[英]Convert NSArray to NSDictionary for sectioning
我有這個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]];
更新
現在,我看到您想要的是
Animal
類型的對象,而不是NSDictionary
類型的對象。 這行代碼是animal[@"name"]
需要使用任何訪問方法來獲取該名稱。 我更新了代碼,以為您需要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]];
正在講故事Animal *
在animalArray
,但是出現的錯誤消息似乎表明NSMutuableArray *
對象存儲在animalArray
。
嘗試下面的代碼。
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]);
希望它可以幫助您...!
希望下面是您正在尋找的代碼。
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 ..等
拿字典並為鍵設置對象
NSMutableArray *arr;
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:arr forKey:@"L"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.