所以我有这个方法,我在其中查询一些 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>"
)
最佳答案
问题似乎是 self.profileDetails
在您调用时为零
[self.profileDetails addObject:profile];
没有地方可以alloc/init
该数组,因此不会添加任何内容。你可能可以摆脱你的 tempArray
做
// this
profileDetails = [NSMutableArray arrayWithCapacity:healths.count];
// instead of this
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:healths.count];
关于ios - 属性数组不添加对象,临时数组会。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22359919/