如何获得可可中NSArray的前x个元素?

问题描述:

可可的新手,似乎缺少了一些东西.

New to Cocoa, and seem to be missing something.

NSArray的前x个元素用作另一个NSArray的最优雅/惯用的方法是什么?显然,我可以遍历它们并手动存储它们,但是似乎必须有一种更标准的方法来执行此操作.

What is the most elegant/idiomatic way to obtain the first x elements of an NSArray as another NSArray? Obviously I can iterate through them and store them manually, but it seems like there has to be a more standard method of doing this.

我期望有一个-arrayWithObjectsInRange:或类似的东西,但是什么也看不到...

I was expecting there to be an -arrayWithObjectsInRange: or something similar, but don't see anything...

NSArray* largeArray...// Contains 50 items...

NSArray* smallArray = // fill in the blank     

// smallArray contains first 10 items from largeArray

谢谢!

您可以使用

You can use subarrayWithRange:.

NSArray *smallArray = [largeArray subarrayWithRange:NSMakeRange(0, 10)];