How do I get the first X elements of an unknown size NSArray?

后端 未结 3 860
清酒与你
清酒与你 2021-02-20 01:59

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

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 02:39

    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

提交回复
热议问题