关于 Objective-C 中 NSArray 循环的术语问题

发布于 2024-08-05 02:09:53 字数 603 浏览 1 评论 0原文

当您有一个 NSArray 并且想要评估和更改元素时,您无法从循环内部更改数组。因此,您创建了一个可以更改的可变副本。

代码示例:

NSMutableArray *bin = [NSMutableArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
NSMutableArray *list = [NSMutableArray arrayWithObjects:@"a1", @"b2", @"c3", @"e4", nil];

NSMutableArray *listHolder = list; // can't mutate 'list' within loop so create a holder

for (int i = 0; i < [list count]; i++) {
    [listHolder replaceObjectAtIndex:i withObject:[bin objectAtIndex:i]];
}

第二个数组 listHolder 叫什么?我的意思是,在这种情况下用什么术语来指代数组。

When you have an NSArray and you want to evaluate and change the elements, you can't change the array from inside the loop. So, you create a mutable copy that can be changed.

code example:

NSMutableArray *bin = [NSMutableArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
NSMutableArray *list = [NSMutableArray arrayWithObjects:@"a1", @"b2", @"c3", @"e4", nil];

NSMutableArray *listHolder = list; // can't mutate 'list' within loop so create a holder

for (int i = 0; i < [list count]; i++) {
    [listHolder replaceObjectAtIndex:i withObject:[bin objectAtIndex:i]];
}

What is that second array listHolder called? I mean, what term is used to refer to an array in this context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

dawn曙光 2024-08-12 02:09:53

这是完全有效的:

NSMutableArray *bin = [NSMutableArray arrayWithObjects:@"0", @"1", …, @"7", nil];
NSMutableArray *list = [NSMutableArray arrayWithObjects:@"a1", …, @"e4", nil];

// NSInteger should be used instead of int
for (NSInteger i = 0; i < [list count]; i++) {
    [list replaceObjectAtIndex:i withObject:[bin objectAtIndex:i]];
}

您不允许在 for … inNSEnumerate 循环内更改数组,但使用索引是完全有效的。

令我困扰的是你对指针的误解。
如果这是一个不允许您改变数组的循环,则不会复制数组,而只会复制指向数组的指针,从而有效地修改您不允许的数组。 (我什至不确定这是否有效。)

不要只是复制指针,而是

// can't mutate 'list' within loop so create a holder
NSMutableArray *listHolder = list;

制作一个真实的副本:

NSMutableArray *copy = [[list mutableCopy] autorelease];

如果我真的必须制作一个副本,我会尝试根据其内容来命名它。例如:

NSMutableArray *views;
NSMutableArray *reorderedViews = [views mutableCopy];

// reorder reorderedViews

有时很难找到一个足够好的名称,那么我通常只使用nameCopy

This is perfectly valid:

NSMutableArray *bin = [NSMutableArray arrayWithObjects:@"0", @"1", …, @"7", nil];
NSMutableArray *list = [NSMutableArray arrayWithObjects:@"a1", …, @"e4", nil];

// NSInteger should be used instead of int
for (NSInteger i = 0; i < [list count]; i++) {
    [list replaceObjectAtIndex:i withObject:[bin objectAtIndex:i]];
}

You're not allowed to change the array inside a for … in or NSEnumerate loop, but using an index is perfectly valid.

What troubles me is your misunderstanding of pointers.
If it were a loop in which you weren't allowed to mutate the array this wouldn't copy the array but only the pointer to the array, effectively modifying the array you're not allowed to. (I'm not even sure if this works.)

Instead of just copying the pointer

// can't mutate 'list' within loop so create a holder
NSMutableArray *listHolder = list;

make a true copy:

NSMutableArray *copy = [[list mutableCopy] autorelease];

In case I really have to make a copy I try to name it according to its content. For example:

NSMutableArray *views;
NSMutableArray *reorderedViews = [views mutableCopy];

// reorder reorderedViews

Sometimes it's hard to find a good enough name, then I usually just use nameCopy.

蓝礼 2024-08-12 02:09:53

在这种情况下,listHolder 将被称为副本

不过你的代码有一个错误。这行代码实际上并没有进行复制,它只是让 listHolderlist 都引用同一个数组对象:

NSMutableArray *listHolder = list;

这将是一个实际的副本:

NSMutableArray *listHolder = [list mutableCopy];

请确保您使用 <如果您希望副本是可变的,则 code>mutableCopy 而不仅仅是 copycopy 方法将返回所有可变类的不可变变体,例如 NSMutableSetNSMutableDictionary 等。

另外,正如其他人所指出的,只有在 for (集合中的项目) 循环内,枚举集合才能被改变。在正常的 for (;;) 中,突变是完全可以的,但如果集合中的项目数量发生变化,可能会导致奇怪的结果。

In this context listHolder would be called a copy.

Your code has a bug though. This line is not actually making a copy, it is only letting listHolder and list both reference the same array object:

NSMutableArray *listHolder = list;

This would be an actual copy:

NSMutableArray *listHolder = [list mutableCopy];

Make sure that you use mutableCopy and not just copy if you want the copy to be mutable. The copy method will return immutable variants on all mutable classes such as NSMutableSet, NSMutableDictionary, and so forth.

Also as others have noted it is only inside the for (item in collection) loop that the enumerated collection can not be mutated. In a normal for (;;) mutation is perfectly ok, but can lead to strange result if the number of items in the collection changes.

谁许谁一生繁华 2024-08-12 02:09:53

没有普遍使用的特定风格或通用名称,毕竟它是您的代码,如果有适当的术语,请使用它们。

话虽如此,通常如果在这种情况下没有特定的名称,那么人们会将原始列表称为“源”(src),将最终列表称为“目的地”(dst),就像在内存块传输中一样风格操作。

There is not specific stylistic or common name for this that is universally used, it is your code afterall, and if there appropriate terms for them use them.

Having said that generally if you don't have specific names in this sort of situation then people refer to the original list as the "source" (src) and the final list as "destination" (dst), just like in a memory blitting style operation.

热风软妹 2024-08-12 02:09:53

我将如何引用原始 NSArray 的临时可变副本。

A temporary mutable copy of the original NSArray would be how I would refer to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文