In objectiveC I have an NSArray, let's call it NSArray* largeArray
, and I want to get a new NSArray* smallArray
with just the first x objects
在objectiveC中,我有一个NSArray,让我们称之为NSArray * largeArray,我想得到一个新的NSArray * smallArray只带有前x个对象
...OR in the event that largeArray is already size <= x I just want a copy of the largeArray. So truncating any objects after index x.
...或者如果largeArray已经是<= x,我只想要一个largeArray的副本。因此在索引x之后截断任何对象。
This approach:
NSArray *smallArray = [largeArray subarrayWithRange:NSMakeRange(0, x)];
Was the answer to this very similar question. But it fails with an error if largeArray is already small.
是这个非常相似的问题的答案。但是如果largeArray已经很小,它就会失败并出现错误。
3 个解决方案
#1
27
You could do this...
你可以这样做......
NSArray *smallArray = [largeArray subarrayWithRange:NSMakeRange(0, MIN(x, largeArray.count))];
That will take the first x elements or the full array if it's smaller than x.
如果它小于x,那将采用前x个元素或完整数组。
If largeArray.count is 100.
如果largeArray.count是100。
If x = 110 then it will take the first 100 results. If x = 90 then it will take the first 90 results.
如果x = 110则将获得前100个结果。如果x = 90,那么它将获得前90个结果。
Yep, that works :D
是的,有效:D
#2
1
Fogmeister's answer is perfectly good, but, in situations where the array is often already small enough, that answer will be mildly inefficient since it always makes a copy. Here is a more efficient version:
Fogmeister的答案是非常好的,但是,在阵列通常已经足够小的情况下,这个答案将会轻微低效,因为它总是会复制。这是一个更有效的版本:
NSAarray *smallArray = largeArray;
if (smallArray.count > MAX_NUM_ITEMS)
smallArray = [smallArray subarrayWithRange:NSMakeRange(0, MAX_NUM_ITEMS)];
When the array is already within the limit this version will just make a reference to the existing array.
当数组已经在限制范围内时,此版本将仅引用现有数组。
#3
-5
Here's the obvious long-hand way of doing it:
这是明显的长手方式:
NSMutableArray* smallMutableArray;
if ([largeArray count] <= x) {
smallMutableArray = [largeArray copy];
} else {
for (int i=0; i<x; i++) {
[smallMutableArray addObject:[largeArray objectAtIndex:i]];
}
}
#1
27
You could do this...
你可以这样做......
NSArray *smallArray = [largeArray subarrayWithRange:NSMakeRange(0, MIN(x, largeArray.count))];
That will take the first x elements or the full array if it's smaller than x.
如果它小于x,那将采用前x个元素或完整数组。
If largeArray.count is 100.
如果largeArray.count是100。
If x = 110 then it will take the first 100 results. If x = 90 then it will take the first 90 results.
如果x = 110则将获得前100个结果。如果x = 90,那么它将获得前90个结果。
Yep, that works :D
是的,有效:D
#2
1
Fogmeister's answer is perfectly good, but, in situations where the array is often already small enough, that answer will be mildly inefficient since it always makes a copy. Here is a more efficient version:
Fogmeister的答案是非常好的,但是,在阵列通常已经足够小的情况下,这个答案将会轻微低效,因为它总是会复制。这是一个更有效的版本:
NSAarray *smallArray = largeArray;
if (smallArray.count > MAX_NUM_ITEMS)
smallArray = [smallArray subarrayWithRange:NSMakeRange(0, MAX_NUM_ITEMS)];
When the array is already within the limit this version will just make a reference to the existing array.
当数组已经在限制范围内时,此版本将仅引用现有数组。
#3
-5
Here's the obvious long-hand way of doing it:
这是明显的长手方式:
NSMutableArray* smallMutableArray;
if ([largeArray count] <= x) {
smallMutableArray = [largeArray copy];
} else {
for (int i=0; i<x; i++) {
[smallMutableArray addObject:[largeArray objectAtIndex:i]];
}
}