gpt4 book ai didi

iphone - 移除行为异常的 objectAtIndex

转载 作者:行者123 更新时间:2023-11-28 23:13:42 27 4
gpt4 key购买 nike

嗨,我有一个数组数组,我在添加某个索引后删除了一个对象。不知何故,我认为这表现得很奇怪,或者我可能忽略了一些事情。这是代码。我用 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];但为什么还要从 questionsArray 中删除?我完全迷失在这里。我知道这是逻辑错误,但我似乎找不到问题所在。 :D

最佳答案

questionsArray 是一个数组,数组不包含对象,它们只包含指向真实数据的指针。因此,如果您向数组添加一个项目,不会进行任何复制,只会存储指向原始对象的指针并保留该对象。

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

[questionsArray addObject: [questionSet copy]];

更新

FWIW、questionSetquestionsArray 绝对不会指向同一个对象。这可以在您获得的显示器中看到

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
)
)
)

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

关于iphone - 移除行为异常的 objectAtIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316974/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com