Iphone 删除行为异常的objectAtIndex

Iphone 删除行为异常的objectAtIndex,iphone,objective-c,ios,Iphone,Objective C,Ios,嗨,我有一个数组,在我添加一个对象之后,我正在删除它的某个索引。不知何故,我认为这是奇怪的行为,或者我可能忽略了一些东西。这是密码。我在上面撒了一些nslogs用于跟踪 for (int i = 0; i < easyQuestionsLength; i++) { NSMutableArray *questionSet = [easyQuestions objectAtIndex:i]; for(int j = 0; j < [[easyQuestionsVa

嗨,我有一个数组,在我添加一个对象之后,我正在删除它的某个索引。不知何故,我认为这是奇怪的行为,或者我可能忽略了一些东西。这是密码。我在上面撒了一些nslogs用于跟踪

    for (int i = 0; i < easyQuestionsLength; i++) {
    NSMutableArray *questionSet = [easyQuestions objectAtIndex:i];
    for(int j = 0; j < [[easyQuestionsVariant objectAtIndex:i] count]; j++)
    {
        NSArray *variant = [[easyQuestionsVariant objectAtIndex:i] objectAtIndex:j];
        NSLog(@"questionSet b4 %@",questionSet);

        [questionSet insertObject:variant atIndex:7];
        NSLog(@"questionSet aft ins%@",questionSet);

        [questionsArray addObject:questionSet];
        NSLog(@"questionsArray aft add %@",questionsArray);

        [questionSet removeObjectAtIndex:7];
        NSLog(@"questionsArray aft rem %@",questionsArray);

        easyQCtr++;
    }
}

我只是在这里做一个removeObjectAtIndex[问题集removeObjectAtIndex:7];但为什么也要从问题中删除Sarray?我在这里完全迷路了。我知道这是逻辑错误,但我似乎找不到问题所在我想你会发现questionsArray是一个指向questionSet的指针,而不是一个独立的数组。反之亦然。确保使用“复制”创建:

NSMutableArray *questionSet = [[NSMutableArray alloc] initWithArray:[easyQuestions objectAtIndex:i] copyItems:YES];
questionsArray是一个数组,数组不包含对象,它们只包含指向真实数据的指针。因此,如果向数组中添加一个项,不进行复制,则只存储指向原始对象的指针,并保留该对象

您向questionsArray添加了一个数组对象,但这只是一个新的引用。如果要存储副本,必须执行以下操作:

[questionsArray addObject: [questionSet copy]];
更新 FWIW、questionSet和questionsArray绝对不会指向同一个对象。这可以在您得到的显示器上看到

问题集显示为

(               --> questionSet, containing:
  0,
  1,
  14,
  ...
)
(               --> questionsArray, containing:
  (             --> questionSet (only element of questionsArray), containing:
    0,
    1,
    14,
    ...
  )
)
而questionsArray显示为

(               --> questionSet, containing:
  0,
  1,
  14,
  ...
)
(               --> questionsArray, containing:
  (             --> questionSet (only element of questionsArray), containing:
    0,
    1,
    14,
    ...
  )
)
这表明questionsArray包含一个对象questionSet。您可以从questionSet中删除某些内容并在questionsArray中显示,反之亦然,原因是questionsArray仅包含对
引用,而不是questionSet的副本,并且该描述递归显示所包含数组、集合和字典的所有元素

请看以下简单代码:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *deeplyNested = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", nil];
    NSArray *toplevel = [NSArray arrayWithObject: [NSArray arrayWithObject: deeplyNested]];

    NSLog(@"\n%@", toplevel);

    [[[toplevel lastObject] lastObject] removeObjectAtIndex: 1];
    [deeplyNested addObject: @"thousand"];

    NSLog(@"\n%@", toplevel);

    [pool drain];
    return 0;
}

如您所见,可以通过
[[toplevel lastObject]lastObject]
或直接寻址deeplyNested。结果是一样的,因为它们引用了相同的对象。

您正在创建
问题数组的地方。
?我在循环外部创建问题数组,如下所示。questionsArray=[[NSMutableArray alloc]init];int easyQCtr=0;对于(int i=0;i问题数组和
问题集
做了什么吗?是的,我知道这很混乱。:D无论如何,questionArray将保存问题集的最终输出,它看起来像我的nslog中的一个,上面写着“questionsArray aft add”。questionSet只是一个问题,questionArray将是所有问题集的集合,直到循环停止。似乎
questionsArray aft add
包含不同类型的对象。我不这么认为。questionsArray是一个单独的数组,包含对其他数组的引用,而不是其他数组的副本(在本例中,只有一个)。