创建一个包含多个NSArrays的NSDictionary

时间:2012-05-08 14:10:52

标签: objective-c ios xcode nsarray nsdictionary

我想将几个NSArrays合并为一个NSDictionary。 这是我的阵列:

@property (nonatomic, strong) NSArray *EuropeTable;
@property (nonatomic, strong) NSArray *AsiaTable;
@property (nonatomic, strong) NSArray *AfricaTable;
@property (nonatomic, strong) NSArray *LatinAmericaTable;
@property (nonatomic, strong) NSArray *NorthAmericaTable;

关于如何做到这一点的任何想法?

*的修改

我希望每个数组都具有相同的密钥@“country”,以便我以后可以使用此单个密​​钥

3 个答案:

答案 0 :(得分:3)

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:self.EuropeTable forKey:@"EuropeTable"];
[dictionary setObject:self.AsiaTable forKey:@"AsiaTable"];
[dictionary setObject:self.AfricaTable forKey:@"AfricaTable"];
[dictionary setObject:self.LatinAmericaTable forKey:@"LatinAmericaTable"];
[dictionary setObject:self.NorthAmericaTable forKey:@"NorthAmericaTable"];

然后访问您需要的其中一个阵列:

NSArray *europeTable = [dictionary objectForKey:@"EuropeTable"];


更新:

NSArray *array = [NSArray arrayWithObjects:self.EuropeTable,
                                           self.AsiaTable,
                                           self.AfricaTable, 
                                           self.LatinAmericaTable,
                                           self.NorthAmericaTable, nil];

NSDictionary *dict = [NSDictionary dictionaryWithObject:array forKey:@"country"];

答案 1 :(得分:2)

NSDictionary *countries =
  [NSDictionary dictionaryWithObjectsAndKeys:
    EuropeTable, @"You",
    AsiaTable, @"Need To",
    AfricaTable, @"Provide",
    LatinAmericaTable @"More",
    NorthAmericaTable, @"Details",
    nil];

如果没有更多细节,我不确定任何答案对你有多大帮助:)

答案 2 :(得分:0)

NSMutableDictionary dict = [NSMutableDictionary dictionary];
[dict addObject:self.EuropeTable forKey:@"EuropeTable"];
etc...