0

I am wondering whether I can copy a certain amount of objects but not all from an array to another array. For example: bigArray has 10 objects, i just want to copy 4 objects from it into smallArray. Please help me out. Thanks a lot

NSMutableArray *smallArray = [bigArray mutableCopy];
1
  • do they have to be random objects? or just the first 4? Commented Nov 22, 2011 at 10:15

3 Answers 3

4

Use the following method...

- (NSArray *)subarrayWithRange:(NSRange)range

As per Apple docs...

Returns a new array containing the receiving array’s elements that fall within the limits specified by a given range.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do like this.

NSMutableArray *smalllArray=[[NSMutableArray alloc] initWithArray:[bigArray subarrayWithRange:NSMakeRange(0, 5)]];

Comments

0

Depending on which objects you want to include in your sub-array you can either make the new array as a subset containing a specific range of the big array or you can base the sub array on certain criteria by making a search predicate.

For the contiguous range, use:

- (NSArray *)subarrayWithRange:(NSRange)range

For a more sophisticated selection, use:

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.