嗨我有一个数组数组,我在添加它后删除某个索引处的对象。不知怎的,我认为这表现得很奇怪,或者我可能忽略了一些东西。这是代码。我将它与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++;
}
}
这是调试控制台输出:
2011-09-06 16:17:33.498 EasyQuiz[25584:10d03] questionSet b4 (
0,
1,
14,
"[1] The value of the digit {#} in {#} is __________.",
none,
2,
1
)
2011-09-06 16:17:33.499 EasyQuiz[25584:10d03] questionSet aft (
0,
1,
14,
"[1] The value of the digit {#} in {#} is __________.",
none,
2,
1,
{
choice = (
70000,
7000,
700,
70
);
var = (
7,
70348
);
}
)
2011-09-06 16:17:33.500 EasyQuiz[25584:10d03] questionsArray aft add (
(
0,
1,
14,
"[1] The value of the digit {#} in {#} is __________.",
none,
2,
1,
{
choice = (
70000,
7000,
700,
70
);
var = (
7,
70348
);
}
)
)
2011-09-06 16:17:33.500 EasyQuiz[25584:10d03] questionsArray aft rem (
(
0,
1,
14,
"[1] The value of the digit {#} in {#} is __________.",
none,
2,
1
)
)
我这里只做一个removeObjectAtIndex [questionSet removeObjectAtIndex:7];但为什么还要从问题中删除?我完全迷失在这里。我知道这是逻辑错误,但我似乎无法找到问题。 :d
答案 0 :(得分:2)
questionsArray 是一个数组,数组不包含对象,它们只包含指针到实际数据。因此,如果向数组添加项目,不进行复制,则仅存储指向原始对象的指针并保留对象。
您向 questionsArray 添加了一个数组对象,但这只是一个新引用。如果您想存储副本,则必须执行以下操作:
[questionsArray addObject: [questionSet copy]];
FWIW, questionSet 和 questionsArray 绝对不会指向同一个对象 。这可以在你得到的显示中看到
questionSet 显示为
( --> questionSet, containing:
0,
1,
14,
...
)
而 questionsArray 显示为
( --> 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;
}
输出结果为:
2011-09-06 12:36:44.966 NestedArrays[12553:707]
(
(
(
one,
two,
three
)
)
)
2011-09-06 12:36:44.968 NestedArrays[12553:707]
(
(
(
one,
three,
thousand
)
)
)
如您所见, deepNested 可以通过[[toplevel lastObject] lastObject]
或直接解决。结果是一样的,因为它们引用了同一个对象。
答案 1 :(得分:0)
我想你会发现questionsArray是一个指向questionSet的指针,而不是一个独特的实际数组。或相反亦然。确保使用副本创建:
NSMutableArray *questionSet = [[NSMutableArray alloc] initWithArray:[easyQuestions objectAtIndex:i] copyItems:YES];