通过循环将NSMutableDictionary添加到数组会导致覆盖以前的值

时间:2014-10-01 12:14:15

标签: parse-platform nsmutablearray nsmutabledictionary

我从Parse.com获得了一系列对象,我试图迭代并拉出objectId。我的方法如下:

-(void)buildActivityDictionary{
    arrayOfArrays = [[NSMutableArray alloc] init];
    arrayForDicts = [[NSMutableArray alloc] init];
    activityDict = [[NSMutableDictionary alloc] init];
    for (PFObject *object in someArray){
        PFObject *fromUser = [object objectForKey:@"fromUser"];
        PFObject *toUser = [object objectForKey:@"toUser"];

        NSString *requestId = object.objectId;
        NSString *fromUserId = fromUser.objectId;
        NSString *toUserId = toUser.objectId;

        [activityDict setObject:requestId forKey:@"requestId"];
        [activityDict setObject:fromUserId forKey:@"fromUserId"];
        [activityDict setObject:toUserId forKey:@"toUserId"];
        [arrayForDicts addObject:activityDict];
        [arrayOfArrays addObject:arrayForDicts];
    }
    NSLog(@"\narrayFofArrays = %@", arrayOfArrays);
}

如何达到预期效果?我尝试使用setValue代替,但没有骰子。此外,当我尝试将每个数组添加到新数组[arrayOfarrays addObject:arrayForDicts]时,我得到相同的结果。这是我日志的输出。任何建议都将不胜感激。

arrayForDicts = (
    {
    fromUserId = lVlLN0ucKQ;
    requestId = SSrFp9pV1Y;
    toUserId = SG359LXCtA;
},
    {
    fromUserId = lVlLN0ucKQ;
    requestId = SSrFp9pV1Y;
    toUserId = SG359LXCtA;
},
    {
    fromUserId = lVlLN0ucKQ;
    requestId = SSrFp9pV1Y;
    toUserId = SG359LXCtA;
},
    {
    fromUserId = lVlLN0ucKQ;
    requestId = SSrFp9pV1Y;
    toUserId = SG359LXCtA;
}

修改

someArray = (
    "<Activity:PMZO9LJ3os:(null)> {\n    ACL = \"<PFACL: 0x1579c520>\";\n    activityType = request;\n    fromUser = \"<PFUser:rJWrNWO15W>\";\n    toUser = \"<PFUser:SG359LXCtA>\";\n}",
    "<Activity:aV3zz4iVrE:(null)> {\n    ACL = \"<PFACL: 0x157e6960>\";\n    activityType = request;\n    fromUser = \"<PFUser:SG359LXCtA>\";\n    toUser = \"<PFUser:SG359LXCtA>\";\n}",
    "<Activity:K7504CAYnl:(null)> {\n    ACL = \"<PFACL: 0x157e7810>\";\n    activityType = request;\n    fromUser = \"<PFUser:xMbXLMIv99>\";\n    toUser = \"<PFUser:SG359LXCtA>\";\n}",
    "<Activity:SSrFp9pV1Y:(null)> {\n    ACL = \"<PFACL: 0x157e72c0>\";\n    activityType = request;\n    fromUser = \"<PFUser:lVlLN0ucKQ>\";\n    toUser = \"<PFUser:SG359LXCtA>\";\n}"
)

1 个答案:

答案 0 :(得分:1)

我修改了你的代码:

-(void)buildActivityDictionary{

arrayOfArrays = [[NSMutableArray alloc] init];
arrayForDicts = [[NSMutableArray alloc] init];

for (PFObject *object in someArray){

    NSMutableDictionary *activityDict = [[NSMutableDictionary alloc] init];
    PFObject *fromUser = [object objectForKey:@"fromUser"];
    PFObject *toUser = [object objectForKey:@"toUser"];

    NSString *requestId = object.objectId;
    NSString *fromUserId = fromUser.objectId;
    NSString *toUserId = toUser.objectId;

    [activityDict setObject:requestId forKey:@"requestId"];
    [activityDict setObject:fromUserId forKey:@"fromUserId"];
    [activityDict setObject:toUserId forKey:@"toUserId"];
    [arrayForDicts addObject:activityDict];
   // [arrayOfArrays addObject:arrayForDicts];
    }
     NSLog(@"arrayForDicts = %@", arrayForDicts);
}

在循环内创建一个字典。在你的代码中,你在循环之外创建字典。每次循环迭代它都会覆盖它的旧值。因为这个原因你的字典数组只包含最后一个对象。

还有一件事,而不是这两个陈述

    PFObject *fromUser = [object objectForKey:@"fromUser"];
    PFObject *toUser = [object objectForKey:@"toUser"];

你也可以写:

    PFUser *fromUser = [object objectForKey:@"fromUser"];
    PFUser *toUser = [object objectForKey:@"toUser"];

来自用户&#34;来自用户&#34;和&#34;到用户&#34;是一个PFUser类型,如someArray Log中所示。