• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在ios中添加带有字典键的数组

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:54:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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


}

@end

@implementation AnimalTableTableViewController

- (id)initWithStyleUITableViewStyle)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", @"anda", @"eacock", @"ig", @"latypus", @"olar 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"],
                @"" : @[@"anda", @"eacock", @"ig", @"latypus", @"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] sortedArrayUsingSelectorselector(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 *)getImageFilenameNSString *)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)numberOfSectionsInTableViewUITableView *)tableView
{
    // Return the number of sections.
    return [animTitles count];
}

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

- (NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section
{
    return [animalSectionTitles objectAtIndex:section];
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 *)sectionIndexTitlesForTableViewUITableView *)tableView
{
  //  return animalSectionTitles;
    return 0;
}

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

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



Best Answer-推荐答案


尝试像这样替换你的 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);
    }

关于ios - 在ios中添加带有字典键的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368186/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap