Question: How do I get the index of the current Object in an NSEnumerator iteration?
问:如何在NSEnumerator迭代中获得当前对象的索引?
(I don't want to keep track of things using an integer counter or use a for loop due to speed reasons. I did it before I just cannot remember how I did it...)
(由于速度的原因,我不希望使用整数计数器来跟踪或使用for循环。我做这件事之前,我就是想不起来我是怎么做的……
2 个解决方案
#1
2
It is doubtful that using an integer counter in a for
loop will cause speed problems. It is more likely to be slower to try and find the index of a given object from an enumerator than it is to just keep a record of the index yourself. If you want to bypass repeated message dispatch, have a look at NSArray's getObjects:range:
method.
在for循环中使用整数计数器是否会导致速度问题值得怀疑。尝试从枚举器中查找给定对象的索引要比自己记录索引慢得多。如果您想绕过重复消息分派,请查看NSArray的getObjects:range: method。
size_t count = [myArray count];
id *objects = calloc(count, sizeof(id));
[myArray getObjects:objects range:NSMakeRange(0, count)];
// if you have a very large count, you'll save doing an
// "objectAtIndex:" for each item.
for (int i = 0; i < count; i++)
[objects[i] doSomething];
free(objects);
You'll probably only see a minimal performance difference for incredibly large arrays, but don't underestimate the optimisations under the hood. Even the documentation for getObjects:range:
discourages using this technique for this purpose.
对于非常大的数组,您可能只会看到最小的性能差异,但是不要低估优化的作用。甚至getObjects的文档:range:阻止为此目的使用这种技术。
NSArray's indexOfObject:
will iterate over all the elements until one returns YES
from isEqual:
message (which may include further message sending).
NSArray的indexOfObject:将遍历所有元素,直到其中一个从isEqual: message返回YES(可能包括进一步的消息发送)。
#2
1
You are asking for an index, so we can assume that you are using an array. If so:
您正在请求一个索引,因此我们可以假设您正在使用一个数组。如果是这样:
[array indexOfObject:currentObject];
will work for most cases (but not all). It would be faster overall to switch to using a normal for loop with an int counter, however.
将适用于大多数情况(但不是全部)。不过,总的来说,使用带有int计数器的普通for循环会更快。
#1
2
It is doubtful that using an integer counter in a for
loop will cause speed problems. It is more likely to be slower to try and find the index of a given object from an enumerator than it is to just keep a record of the index yourself. If you want to bypass repeated message dispatch, have a look at NSArray's getObjects:range:
method.
在for循环中使用整数计数器是否会导致速度问题值得怀疑。尝试从枚举器中查找给定对象的索引要比自己记录索引慢得多。如果您想绕过重复消息分派,请查看NSArray的getObjects:range: method。
size_t count = [myArray count];
id *objects = calloc(count, sizeof(id));
[myArray getObjects:objects range:NSMakeRange(0, count)];
// if you have a very large count, you'll save doing an
// "objectAtIndex:" for each item.
for (int i = 0; i < count; i++)
[objects[i] doSomething];
free(objects);
You'll probably only see a minimal performance difference for incredibly large arrays, but don't underestimate the optimisations under the hood. Even the documentation for getObjects:range:
discourages using this technique for this purpose.
对于非常大的数组,您可能只会看到最小的性能差异,但是不要低估优化的作用。甚至getObjects的文档:range:阻止为此目的使用这种技术。
NSArray's indexOfObject:
will iterate over all the elements until one returns YES
from isEqual:
message (which may include further message sending).
NSArray的indexOfObject:将遍历所有元素,直到其中一个从isEqual: message返回YES(可能包括进一步的消息发送)。
#2
1
You are asking for an index, so we can assume that you are using an array. If so:
您正在请求一个索引,因此我们可以假设您正在使用一个数组。如果是这样:
[array indexOfObject:currentObject];
will work for most cases (but not all). It would be faster overall to switch to using a normal for loop with an int counter, however.
将适用于大多数情况(但不是全部)。不过,总的来说,使用带有int计数器的普通for循环会更快。