简体   繁体   English

如何创建NSArray的NSArray

[英]How to create an NSArray of NSArray

I am trying to sort an array of NSDictionaries into an alphabatised array of arrays based of one of the NSDictionary valueForKey: first letters 我正在尝试将NSDictionaries的数组排序为基于NSDictionary valueForKey:的字母数组的首字母排序的数组valueForKey:首字母

effectively I want something that looks a little something like this 实际上,我想要看起来像这样的东西

Array A // contains NSDictionaies with keyvalue MAF strings starting with A
 NSDictionary1
 NSDictionary2
 NSDictionary3
Array B // contains NSDictionaies with keyvalue MAF strings starting with B
 NSDictionary1
 NSDictionary2
 NSDictionary3
Array C // contains NSDictionaies with keyvalue MAF strings starting with C
 NSDictionary1
 NSDictionary2
 NSDictionary3
etc

So I have an array of Letters and inside each letter array I would like to have an Array of NSDictionary values that keyValue MAFs first letter should match. 因此,我有一个Letters数组,并且在每个letter数组中都希望有一个KeyValue MAF的第一个字母应该匹配的NSDictionary值数组。

So far I have an array of dictionaries that I have sorted like so 到目前为止,我整理了一些字典,就像这样

NSSortDescriptor *sortDescriptor;
        sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"MAF"
                                                      ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        sortedDictionaryArray = [arrayData sortedArrayUsingDescriptors:sortDescriptors]; // use this in cellforrow: and didselectrow:

        NSLog(@"%@", sortedDictionaryArray); // this gives me a sorted array of NSdictionaries based off their MAF keyvalue.

This is what the NSDicitonary looks like 这就是NSDicitonary的样子

HADM = 1;
ISLOT = 0;
ISVERT = 0;
MAF = "Yazot"; // keyvalue I am trying to work with.

Then I get stuck, I am not sure where to go from here. 然后我被卡住了,我不确定从这里去哪里。 if anyone could help me understand how to create an NSArray of NSArrays NSDictionaries that would be great! 如果有人可以帮助我理解如何创建NSArrays NSSDs NSDictionaries的NSArray,那就太好了! hopefully I have explained what I am trying to do correctly, if you need any more details just let me know and I will supply. 希望我已经解释了我要正确执行的操作,如果您需要更多详细信息,请告诉我,我会提供。

This code will first sort your array of dictionaries by using the objects assigned to the key @"MAF" . 此代码将首先使用分配给键@"MAF"的对象对字典数组进行排序。 Than it will create a new Dictionary where it will add a mutable array for each first letter of that string and add the object to it. 然后,它将创建一个新的Dictionary,在字典中为该字符串的每个第一个字母添加一个可变数组,并将对象添加到其中。

NSArray *array = @[@{@"MAF": @"Yazot"}, @{@"MAF": @"Lorem"},@{@"MAF": @"Ipsum"}, @{@"MAF": @"Laura"},@{@"MAF": @"Isit"}];

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    return [obj1[@"MAF"] compare:obj2[@"MAF"]];
}];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for (NSDictionary *dict in sortedArray) {
    NSString *firstLetter = [dict[@"MAF"] substringToIndex:1];

    if(![[dictionary allKeys] containsObject:firstLetter])
        dictionary[firstLetter] = [NSMutableArray array];

    [dictionary[firstLetter] addObject:dict];

}

if you need case insensitive string handling, "L" and "l" should be put into the same array, do 如果需要不区分大小写的字符串处理,则应将“ L”和“ l”放入同一数组中,

NSArray *array = @[@{@"MAF": @"Yazot"}, @{@"MAF": @"Lorem"},@{@"MAF": @"Ipsum"}, @{@"MAF": @"Laura"},@{@"MAF": @"Isit"}];

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2) {
    return [obj1[@"MAF"] caseInsensitiveCompare:obj2[@"MAF"]];
}];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

for (NSDictionary *dict in sortedArray) {
    NSString *firstLetter = [dict[@"MAF"] substringToIndex:1];
    firstLetter = [firstLetter lowercaseString];
    if(![[dictionary allKeys] containsObject:firstLetter])
        dictionary[firstLetter] = [NSMutableArray array];

    [dictionary[firstLetter] addObject:dict];

}

dictionary results in 字典结果

{
    i =     (
                {
            MAF = Ipsum;
        },
                {
            MAF = Isit;
        }
    );
    l =     (
                {
            MAF = Laura;
        },
                {
            MAF = Lorem;
        }
    );
    y =     (
                {
            MAF = Yazot;
        }
    );
}

We can start by creating a dictionary of arrays of dictionaries. 我们可以从创建字典数组的字典开始。

NSMutableDictionary *dictsForFirstLetter = [[NSMutableDictionary alloc] init];

for (NSDictionary *dict in arrayData) {
    NSString *dictMAF = dict[@"MAF"];
    NSString *firstLetter = [[dictMAF substringToIndex:1] uppercaseString];
    NSMutableArray *dicts = dictsForFirstLetter[firstLetter];
    if (!dicts) {
        dicts = [[NSMutableArray alloc] init];
        dictsForFirstLetter[firstLetter] = dicts;
    }
    [dicts addObject:dict];
}

Then you can retrieve the array of dictionaries by the first letter of their MAF value: 然后,您可以按其MAF值的第一个字母检索字典数组:

NSArray *dicts = dictsForFirstLetter[@"Y"];

To get an alphabetized array from this, you can sort the dictionary's keys and enumerate over them: 要从中获得按字母顺序排列的数组,可以对字典的键进行排序并枚举它们:

NSMutableArray *alphabetizedDicts = [[NSMutableArray alloc] init];

NSArray *firstLetters = [dictsForFirstLetter allKeys];
NSArray *sortedFirstLetters = [firstLetters sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

for (NSString *firstLetter in sortedFirstLetters) {
    NSArray *dicts = dictsForFirstLetter[firstLetter];
    [alphabetizedDicts addObject:dicts];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM