【问题标题】:Adding arrays with key to dictionary in ios在ios中添加带有字典键的数组
【发布时间】:2016-03-25 21:42:36
【问题描述】:

我是 iOS 新手,正在开发一个应用程序。在这个我有一个包含标题的数组(a,b,c,....)作为值,在另一个数组中我有一个包含动物名称作为字符串的值,现在我想将这些值作为键值存储在字典,意思是动物的名字以“a”开头应该像这样添加,

 NSDictionary *animals;
    NSArray *animalSectionTitles;
    //Edit by me
    NSMutableDictionary *anim;
    NSArray *animTitles;
    NSMutableArray *commmonary;
    NSMutableArray *myary;


}

@end

@implementation AnimalTableTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    anim = [[NSMutableDictionary alloc]init];
   // animals = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu", @"Giraffe", @"Greater Rhea", @"Hippopotamus", @"Horse", @"Koala", @"Lion", @"Llama", @"Manatus", @"Meerkat", @"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear", @"Rhinoceros", @"Seagull", @"Tasmania Devil", @"Whale", @"Whale Shark", @"Wombat"];
    animals = @{
                @"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"],
                @"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
                @"C" : @[@"Camel", @"Cockatoo"],
                @"D" : @[@"Dog", @"Donkey"],
                @"E" : @[@"Emu"],
                @"G" : @[@"Giraffe", @"Greater Rhea"],
                @"H" : @[@"Hippopotamus", @"Horse"],
                @"K" : @[@"Koala"],
                @"L" : @[@"Lion", @"Llama"],
                @"M" : @[@"Manatus", @"Meerkat"],
                @"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
                @"R" : @[@"Rhinoceros"],
                @"S" : @[@"Seagull"],
                @"T" : @[@"Tasmania Devil"],
                @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};


    commmonary = [NSMutableArray arrayWithObjects:@"Bear", @"Buffalo",@"Camel", @"Cockatoo",@"Dog",@"Donkey",@"Emu",@"Giraffe",@"Hippopotamus", @"Horse",@"Koala",@"Lion",@"Llama",@"Lion", @"Llama",@"Manatus",@"Meerkat",@"Panda",@"Peacock",@"Pig", @"Platypus",@"Rhinoceros",@"Seagull",@"Seagull",@"Whale",@"Wombat", nil];

    animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

   //adding alll keys in array from dictionary

    animTitles = [animals allKeys];
    NSLog(@"===My all keys for dictionary----%@",animTitles);
    NSLog(@"===my anim common aray is---%li",commmonary.count);

//new logic..!
    for(NSString *letter in animTitles)
    {
        [anim setObject:[NSMutableArray array] forKey:letter];
    }

    for(NSString *animal in commmonary)
    {
        NSString *firstLetter = [animal substringToIndex:1];
        NSMutableArray *arr = [anim objectForKey:firstLetter];
        [arr addObject:animal];


          [anim setObject: arr forKey: firstLetter];

           NSLog(@"==my array is==%@",anim);
    }

}



//new logic..!!



- (NSString *)getImageFilename:(NSString *)animal
{
    NSString *imageFilename = [[animal lowercaseString] stringByReplacingOccurrencesOfString:@" " withString:@"_"];
    imageFilename = [imageFilename stringByAppendingString:@".jpg"];

    return imageFilename;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [animTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString *sectionTitle = [animTitles objectAtIndex:section];
    NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
    return [sectionAnimals count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [animalSectionTitles objectAtIndex:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    // Configure the cell...
    NSString *sectionTitle = [animTitles objectAtIndex:indexPath.section];
    NSArray *sectionAnimals = [anim objectForKey:sectionTitle];
    NSString *animal = [sectionAnimals objectAtIndex:indexPath.row];
    cell.textLabel.text = animal;
    cell.imageView.image = [UIImage imageNamed:[self getImageFilename:animal]];

    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
  //  return animalSectionTitles;
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [animalSectionTitles indexOfObject:title];
}

这里 animTitles 包含 a,c,d...作为值。提前致谢。

【问题讨论】:

    标签: ios objective-c arrays dictionary


    【解决方案1】:

    尝试像这样替换你的 for 循环

       for (int i=0; i<animTitles.count; i++) {
    
            myary = [[NSMutableArray alloc]init];
            for(int j=0;j<commmonary.count;j++){
                NSString *a = [commmonary objectAtIndex:i];
                NSString *firstLetter = [a substringToIndex:1];
                if ([firstLetter isEqualToString: [animTitles objectAtIndex:i]]) {
                    //adding values to array.
                    [myary addObject:a];
                }
            }
            [anim setObject: myary forKey: [commmonary objectAtindex:i]];
            NSLog(@"====my Dictionary is===%@-->",anim);
        }
    

    【讨论】:

      【解决方案2】:

      这会奏效。 TESTED:

      anim = [[NSMutableDictionary alloc] init];
      
      animTitles = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
      
      commmonary = [NSMutableArray arrayWithObjects:@"Bear",  @"Buffalo",@"Camel", @"Cockatoo",@"Dog",@"Donkey",@"Emu",@"Giraffe",@"Hippopotamus", @"Horse",@"Koala",@"Lion",@"Llama",@"Lion", @"Llama",@"Manatus",@"Meerkat",@"Panda",@"Peacock",@"Pig", @"Platypus",@"Rhinoceros",@"Seagull",@"Seagull",@"Whale",@"Wombat", nil];
      
      for(NSString *letter in animTitles)
      {
          [anim setObject:[NSMutableArray array] forKey:letter];
      }
      
      for(NSString *animal in commmonary)
      {
          NSString *firstLetter = [animal substringToIndex:1];
          NSMutableArray *arr = [anim objectForKey:firstLetter];
          [arr addObject:animal];
      }
      

      【讨论】:

      • 有几件事没有得到......什么是“动物”?\
      • 像魅力一样工作..谢谢哥们..:)
      • 你能帮我解决类似的问题吗?
      • 我得到错误的有序动物名称与标题不匹配..在“C”动物中表示“r”和其他人一样
      • 是的......实际上我正在制作这种演示,但我的实际代码非常复杂。我正在通过编辑问题来更新你。
      【解决方案3】:

      创建一个 NSDictionary

      NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
      NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
      NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                             forKeys:keys];
      
      NSMutableDictionary *mutableDict = [dictionary mutableCopy];  // for mutable
      

      【讨论】:

        【解决方案4】:
        NSArray* a = @[@"Affrican cat", @"Assian cat", @"Alsesian fox"];
        NSArray* b = @[@"Bear", @"Black Swan", @"Buffalo"];
        
        NSDictionary* dict = [NSDictionary dictionaryWithObjects:b forKeys:a];
        

        【讨论】:

          猜你喜欢
          • 2014-09-05
          • 1970-01-01
          • 1970-01-01
          • 2020-05-30
          • 1970-01-01
          • 2020-05-02
          • 2018-10-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多