关于 Objective-C 中 NSArray 循环的术语问题
当您有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是完全有效的:
您不允许在
for … in
或NSEnumerate
循环内更改数组,但使用索引是完全有效的。令我困扰的是你对指针的误解。
如果这是一个不允许您改变数组的循环,则不会复制数组,而只会复制指向数组的指针,从而有效地修改您不允许的数组。 (我什至不确定这是否有效。)
不要只是复制指针,而是
制作一个真实的副本:
如果我真的必须制作一个副本,我会尝试根据其内容来命名它。例如:
有时很难找到一个足够好的名称,那么我通常只使用
nameCopy
。This is perfectly valid:
You're not allowed to change the array inside a
for … in
orNSEnumerate
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
make a true copy:
In case I really have to make a copy I try to name it according to its content. For example:
Sometimes it's hard to find a good enough name, then I usually just use
nameCopy
.在这种情况下,
listHolder
将被称为副本。不过你的代码有一个错误。这行代码实际上并没有进行复制,它只是让
listHolder
和list
都引用同一个数组对象:这将是一个实际的副本:
请确保您使用 <如果您希望副本是可变的,则 code>mutableCopy 而不仅仅是
copy
。copy
方法将返回所有可变类的不可变变体,例如NSMutableSet
、NSMutableDictionary
等。另外,正如其他人所指出的,只有在 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
andlist
both reference the same array object:This would be an actual copy:
Make sure that you use
mutableCopy
and not justcopy
if you want the copy to be mutable. Thecopy
method will return immutable variants on all mutable classes such asNSMutableSet
,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 normalfor (;;)
mutation is perfectly ok, but can lead to strange result if the number of items in the collection changes.没有普遍使用的特定风格或通用名称,毕竟它是您的代码,如果有适当的术语,请使用它们。
话虽如此,通常如果在这种情况下没有特定的名称,那么人们会将原始列表称为“源”(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.
我将如何引用原始 NSArray 的临时可变副本。
A temporary mutable copy of the original NSArray would be how I would refer to it.