繁体   English   中英

尝试将数据存储在目标c中的2d数组中

[英]Trying to store data in an 2d array in objective c

我的代码可以运行,但是随后我必须为我的应用程序支持的每个新国家/地区添加一个新的数组和新代码,这可能效率很低,因为必须在应用程序启动并显示视图之前运行此代码。

- (NSMutableDictionary *)retrieveDictionaryOfCountryAndCompany {
    NSMutableDictionary *companyAndCountry = [[NSMutableDictionary alloc] init];
    NSMutableArray *countryNames = [[NSMutableArray alloc] init];
    NSMutableArray *germanyArray = [[NSMutableArray alloc] init];
    NSMutableArray *englandArray = [[NSMutableArray alloc] init];

    PFQuery *companyQuery = [PFQuery queryWithClassName: @"Company"];
    NSArray *objects = [companyQuery findObjects];
    for (PFObject *object in objects) {
        if ([[object valueForKey:@"country"]  isEqual: @"Germany"]) {
            [germanyArray addObject:[object valueForKey:@"name"]];
        }

        [countryNames addObject:[object valueForKey:@"country"]];
    }

    NSSet *uniqueItems = [NSSet setWithArray:germanyArray];
    germanyArray = [[uniqueItems allObjects] mutableCopy];
    uniqueItems = [NSSet setWithArray:countryNames];
    countryNames = [[uniqueItems allObjects] mutableCopy];
    [germanyArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    [countryNames sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    [companyAndCountry setObject:germanyArray forKey:@"Germany"];
    [companyAndCountry setObject:countryNames forKey:@"country"];
    return companyAndCountry;
}

是否有更好的解决方案,因为它太简单了,所以我没有看到? 谢谢你的帮助。

除非我很稠密:

- (NSMutableDictionary *)retrieveDictionaryOfCountryAndCompany {
    NSMutableArray *countryNames = [NSMutableArray array];
    NSMutableDictionary *companyAndCountry =
        [@{ @"country" : countryNames } mutableCopy];

    PFQuery *companyQuery = [PFQuery queryWithClassName: @"Company"];
    NSArray *objects = [companyQuery findObjects];
    for (PFObject *object in objects) {
        NSString *countryName = [object valueForKey:@"country"];
        NSString *placeName = [object valueForKey:@"name"];

        if(!countryName || !placeName) continue; // a malformed record
        [countryNames addObject:countryName];

        NSMutableSet *namesInCountry = companyAndCountry[countryName];
        if(!namesInCountry)
        {
            namesInCountry = [NSMutableSet set];
            companyAndCountry[countryName] = namesInCountry;
        }

        [namesInCountry addObject:placeName];
    }

    for (NSString *key in [[companyAndCountry allKeys] copy])
    {
        NSArray *sortedArray = 
               [[companyAndCountry[key] allObjects] 
                   sortedArrayUsingSelector:
                        @selector(localizedCaseInsensitiveCompare:)];
        companyAndCountry[key] = sortedArray;
    }

    return companyAndCountry;
}

因此,只需按需创建并填充列表即可,起初是将其设置为集合,以避免最后出现一些数组->设置->数组->排序后的数组乱序的情况。

至于性能,Parse是一个Web服务,因此应该全部异步完成。 是这样吗

暂无
暂无

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

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