首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数组中的数据拆分和排序到nsmutabledictionary

如何将数组中的数据拆分和排序到nsmutabledictionary
EN

Stack Overflow用户
提问于 2015-05-05 00:19:44
回答 3查看 36关注 0票数 0

我有这样的数组数据。

代码语言:javascript
复制
  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"];

以及如何按nsmutabledictionary的第一个字母分组。(依赖于数组数据)如下所示。

代码语言:javascript
复制
 animals = @{@"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"]};

谢谢。!!

EN

回答 3

Stack Overflow用户

发布于 2015-05-05 00:31:00

这很容易自己完成,但不管怎样,请检查这段代码

代码语言:javascript
复制
NSArray *words = @[@"AS", @"CS", @"AQ", @"CA", @"SA"];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (NSString *word in words) {

    if (word.length > 0) {

        NSString *firstChar = [word substringToIndex:1];
        NSMutableArray *contents = dic[firstChar];
        if (!contents) {
            contents = [NSMutableArray array];
            dic[firstChar] = contents;
        }

        [contents addObject:word];
    }


}
票数 1
EN

Stack Overflow用户

发布于 2015-05-05 00:29:17

你需要自己去做。迭代数组,从项中获取第一个字符,检查它是否存在于字典中,如果存在,则添加到数组中,如果不存在,则插入一个新数组。

任何替代方法都可以使用提供此功能的第三方库。

票数 0
EN

Stack Overflow用户

发布于 2015-05-05 00:30:33

代码语言:javascript
复制
NSArray *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"];

NSMutableDictionary *animalDict = [NSMutableDictionary dictionary];

for (NSString *animal in animals) {
    NSString *firstLetter = [animal substringToIndex:1];
    NSMutableArray *letterArray = [animalDict objectForKey:firstLetter];
    if (!letterArray) {
        letterArray = [NSMutableArray arrayWithObject:animal];
        [animalDict setObject:letterArray forKey:firstLetter];
    } else {
        [letterArray addObject:animal];
        [letterArray sortUsingSelector:@selector(caseInsensitiveCompare:)];
    }
}

NSLog(@"%@", animalDict);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30034890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档