如何返回 NSArray 中指定索引处的对象?

发布于 2024-08-28 21:49:55 字数 128 浏览 2 评论 0原文

示例:

我有一个包含 40 个对象的 NSArray。仅返回索引等于另一个 NSArray 中指定的数字(例如 {0, 27, 36})的对象的最有效方法是什么?

这里可以使用谓词吗?或者有更简单有效的方法吗?

Example:

I have an NSArray with 40 objects. What is the most efficient way to return ONLY those objects with index equal to a number specified in another NSArray (e.g {0, 27, 36} for example)?

Can a predicate be used here? Or is there a more simple and efficient approach?

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

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

发布评论

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

评论(3

黎歌 2024-09-04 21:49:56

有一种方法(objectsAtIndexes) 用于从原始数组返回指定索引,但它需要 NSIndexSet 作为其参数,并且没有内置方法来转换索引数组到一个索引集。从好的方面来说,您可以使用短循环生成索引集,如下所示:

NSMutableIndexSet indexes = [NSMutableIndexSet indexSet];
for (NSNumber * number in indexArray)
{
    [indexes addIndex:[number intValue]];
}
return [originalArray objectsAtIndexes:indexes];

这比简单地循环原始数组更有效吗?我不知道。为了确定,您必须分析您的应用程序。

There is a method (objectsAtIndexes) for returning specified indexes from an original array, but it requires an NSIndexSet as its argument, and there isn't a built-in way to convert your array of indices to an index set. On the plus side, you can generate the index set with a short loop, as follows:

NSMutableIndexSet indexes = [NSMutableIndexSet indexSet];
for (NSNumber * number in indexArray)
{
    [indexes addIndex:[number intValue]];
}
return [originalArray objectsAtIndexes:indexes];

Is this any more efficient than simply looping through the original array? I have no idea. You would have to profile your app to be sure.

清风疏影 2024-09-04 21:49:56

我记得读过一次,我不能说在哪里,但它在我脑海中挥之不去,如果这是你需要做很多次的事情并且数组相当大,那么有一种更有效的方法。您可以从该数组创建一个新字典,其中每个条目都有索引号的键,值是数组项。您只需创建字典一次,因此艰苦的工作一下子就结束了。然后,每次您需要访问某些索引处的值时,您只需询问 valueForKey:@"indexNumber" 即可。但我从来没有需要这个,所以我从未测试过从长远来看它是否更有效......这只是一个想法。

I remember reading one time, and I cant say where but it stuck in my mind, that if this was something you need to do many times and the array is fairly large that there is a more efficient way. You could create a new dictionary from that array where each entry has the key of the index number and the value is the array item. You only create the dictionary once so the hard work is over in one shot. Then every time you need to access values at certain indexes you just ask for valueForKey:@"indexNumber". But I never had a need for this so I never tested if it was more efficient in the long run... it's just a thought.

不再让梦枯萎 2024-09-04 21:49:55

为什么不迭代索引数组并查找数据数组中的每个索引,用查找的对象替换索引。

最后,保存索引的数组现在保存对象。如果您不想擦除索引数组,则只需创建一个与索引数组大小相同的新数组并将对象放在那里。

您可能对这个设计考虑过多,并成为微优化的牺牲品。 (这是一件坏事。)

Why don't you just iterate over the index array and look up each index in the data array replacing the index with the looked-up object.

In the end, the array that held the indices now holds the objects. If you don't want to wipe out your index array, then just create a new array of the same size as the index array and put the objects there.

You might be over-thinking the design of this and falling prey to micro-optimization. (Which is a bad thing.)

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