Property Array没有添加对象,临时数组也没有。

时间:2014-03-12 17:55:30

标签: ios objective-c arrays

所以我有这个方法,我在其中查询一些Singleton类来获取数据,我将它添加到我的可变数组,称为配置文件详细信息,但它没有添加,如果我首先将它添加到临时数组并且然后使用它将它分配给我的配置文件详细信息数组吗?这里发生了什么?

//Property declaration 
@property (nonatomic, strong) NSMutableArray *profileDetails;

//Method definition
- (void) getAllProfiles : (NSArray *) profiles
{
if (_myHealthDetailService == nil) {
    self.myHealthDetailService = [[MyHealthDetailService alloc] init];
}

if (profiles) {

    if (self.currentProfileCounter < [profiles count]) {

        MyHealth *profile = [profiles objectAtIndex:self.currentProfileCounter];

        [self.myHealthDetailService requestForMyHealthDetailWithHealth:profile completion:^(NSArray *healths) {

            self.currentProfileCounter = self.currentProfileCounter + 1;

            if (healths) {

                NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:healths.count];

                for (MyHealth *profile in healths) {

                    NSLog(@"Adding Proile: %@",profile);


                    [self.profileDetails addObject:profile];

                    [tempArray addObject:profile];

                    NSLog(@"Proile details array after adding %@ profile is: %@",profile, self.profileDetails);

                    NSLog(@"Temp Proile details array after adding %@ profile is: %@",profile, tempArray);


                }

                self.profileDetails = tempArray;
                NSLog(@"Proile details array after copying is: %@",self.profileDetails);


            }

            DashboardHealthCell_iPad *cell = (DashboardHealthCell_iPad *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:DashboardRow_iPadTypeHealth inSection:0]];

            NSLog(@"Proile details array is: %@",self.profileDetails);

            [cell populateMyHealthProfiles:self.profileDetails];

        } failureBlock:^(NSError *error) {

        }];
    }
}
}

相关的NSLog输出:

2014-03-12 12:45:58.144 CHM[9768:70b] Proile details array after adding <MyHealth: 0xd562ce0>     profile is: (null)
2014-03-12 12:45:58.144 CHM[9768:70b] Temp Proile details array after adding <MyHealth: 0xd562ce0> profile is: (
"<MyHealth: 0xd562270>",
"<MyHealth: 0xd5629e0>",
"<MyHealth: 0xd562ce0>"
)
2014-03-12 12:45:58.144 CHM[9768:70b] Proile details array after copying is: (
"<MyHealth: 0xd562270>",
"<MyHealth: 0xd5629e0>",
"<MyHealth: 0xd562ce0>"
)

1 个答案:

答案 0 :(得分:2)

问题似乎是self.profileDetails在您调用

时为零
 [self.profileDetails addObject:profile];

没有你alloc / init那个数组的地方,所以什么都没有添加。你可以通过

来摆脱你的tempArray
// this
profileDetails = [NSMutableArray arrayWithCapacity:healths.count];
// instead of this
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:healths.count];