ios - 属性数组不添加对象,临时数组会。

标签 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>"
)

最佳答案

问题似乎是 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/

相关文章:

c++ - getline 不接受任何输入

java - 在 Java 中使用数组求最小值、最大值和平均值

iphone - Objective-C/iphone - 改变像素颜色?

android - 如何为 android 和 iOS GCM 使用相同的服务器 api key

ios - 如何从第三个 viewController 返回到第一个 viewController

ios - AVAudioPlayer 预加载的声音从内存中泄漏

iphone - 仅在应用程序首次启动时使用 plist 预填充核心数据

objective-c - 在哪里可以找到 kCG KeyboardEvent Keycode 西里尔语言键码列表

python - 在 NumPy 数组的可变位置插入新轴

ios - 如何在键盘出现后移动 UITextField?