0

我在 NSArray 中有 NSDictionaries,如下所示。

数组(字典(“用户”:1,“p1”:1),字典(“用户”:2,“p1”:3),字典(“用户”:1,“p1”:5),字典(“用户”:2,“p1”:7))

我想把这个数组变成字典,如下所示。

NSArray *u1 = [NSArray arrayWithObjects:@"1", @"5", nil];
NSArray *u2 = [NSArray arrayWithObjects:@"3", @"7", nil];
keys = [NSArray arrayWithObjects:@"u1", @"u2", nil];
points = [NSDictionary dictionaryWithObjectsAndKeys:u1, @"u1", u2, @"u2", nil];

我怎样才能做到这一点?我迷路了,你们能帮帮我吗?

4

5 回答 5

2

难道你不能只遍历你的原始数组,询问每个字典键“user”的对象是否为 1,如果是,将对象复制到索引 0 处的新数组中?或者,如果您的用户编号按计数顺序排列,甚至可能索引编号等于用户编号。然后重复 "user" = 2 等。然后制作一个字典,以便每个键/对象对由键数组 (keys[i]) 中的键和新数组 (objects[i]) 中的对象创建。

于 2012-12-08T16:52:36.873 回答
1

你试过什么?

这是一些直接输入答案的代码,因此尚未经过测试:

您还没有为原始数组命名,因此我们假设它是:

NSArray *originalArray;

我们需要一个可变字典来存储结果:

NSMutableDictionary *points = [NSMutableDictionary new];

现在我们需要处理原始数组中的每个元素,它是一个字典:

for(NSDictionary *item in originalArray)
{

points获取数组中匹配的当前条目item。您不为您的条目提供类型,因此我们将使用id

   id currentUser = [item objectForKey:@"user"];
   NSMutableArray *currentValues = [points objectForKey:currentUser];

如果这是currentUserthen currentValueswill be的第一次出现nil,我们需要为该p1值创建一个数组并将其添加到points

   if (currentValues == nil)
      [points addObject:[NSMutableArray arrayWithObject:[item objectForKey:@"p1"]
                                                 forKey:currentUser
                        ]
      ]

否则,我们只需将p1值添加到数组中:

   else
      [currentValues setObject:[item objectForKey:@"p1"]];

关闭循环并获取密钥:

}

NSArray *keys = [points allKeys];

现在,如果您使用的是 Xcode 4.5,您可以使用现代语法来处理其中的一些:

NSMutableDictionary *points = [NSMutableDictionary new];
for(NSDictionary *item in originalArray)
{
   id currentUser = item[@"user"];
   NSMutableArray *currentValues = points[currentUser];
   if (currentValues == nil)
      points[currentUser] = [NSMutableArray arrayWithObject:item[@"p1"];
   else
      [currentValues addObject:item[@"p1"]];
}
NSArray *keys = [points allKeys];

高温高压

于 2012-12-08T17:42:59.737 回答
0

大量的方法。这是另一个:

NSArray *originalArray = @[
    @{@"user":@"u1", @"p1":@"1"},
    @{@"user":@"u2", @"p1":@"3"},
    @{@"user":@"u1", @"p1":@"5"},
    @{@"user":@"u2", @"p1":@"7"}
];

NSLog(@"originalArray = %@", originalArray);

NSMutableDictionary *results = [NSMutableDictionary dictionary];

for (NSDictionary *dictionary in originalArray) {
    NSString *user = dictionary[@"user"];
    NSString *p1   = dictionary[@"p1"];

    if (!results[user])
        results[user] = [NSMutableArray array];

    [results[user] addObject:p1];
}

NSLog(@"results = %@", results);

这需要:

originalArray = (
        {
        p1 = 1;
        user = u1;
    },
        {
        p1 = 3;
        user = u2;
    },
        {
        p1 = 5;
        user = u1;
    },
        {
        p1 = 7;
        user = u2;
    }
)

并给出

results = {
    u1 =     (
        1,
        5
    );
    u2 =     (
        3,
        7
    );
}
于 2012-12-08T17:40:08.707 回答
0

另一种可能的解决方案(适用于任意数量的用户):

NSArray *orig = @[
    @{@"user" : @"1", @"p1" : @"1"},
    @{@"user" : @"2", @"p1" : @"3"},
    @{@"user" : @"1", @"p1" : @"5"},
    @{@"user" : @"2", @"p1" : @"7"},
];

// Create set of all users (without duplicates)
NSSet *users = [NSSet setWithArray:[orig valueForKey:@"user"]];

NSMutableDictionary *points = [NSMutableDictionary dictionary];
for (NSString *user in users) {
    // newKey = "u" + username, e.g. "u1" or "u2":
    NSString *newKey = [@"u" stringByAppendingString:user];

    // newValue = array of "p1" values of the current user:
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"user == %@", user];
    NSArray *newValue = [[orig filteredArrayUsingPredicate:pred] valueForKey:@"p1"];

    // Add to dictionary:
    [points setObject:newValue forKey:newKey];
}
NSLog(@"%@", points);

输出:

{
    u1 =     (
        1,
        5
    );
    u2 =     (
        3,
        7
    );
}

并且可以通过以下方式获得密钥

NSArray *keys = [points allKeys];
于 2012-12-08T17:31:41.263 回答
0

你可以这样做(代码未测试)

NSMutableArray *keys=[NSMutableArray new];
NSMutableArray *u1=[NSMutableArray new]; 
NSMutableArray *u2=[NSMutableArray new];
NSMutableDictionary  *points=[NSMutableDictionary new];

for (id dict in array){
    NSString *user=[dict objectForKey:@"user"];
    NSString *p1=[dict objectForKey:@"p1"];
    [keys addObject:[NSString stringWithFormat:@"%@",user]];
    if( [user isEqualToString:@"1"] ){
        [u1 addObject:user];
    }
    else{
        [u2 addObject:user];
    }
}

points=[NSDictionary dictionaryWithObjectsAndKeys:u1,@"u1",u2, @"u2", nil];
于 2012-12-08T17:24:45.377 回答