What is the most elegant way to get a subarray from NSArray?

In Python, I can easily get a subarray.

>> v1 = ['a', 'b', 'c', 'd']
>> v2 = v1[1:]
>> v2
['b', 'c', 'd']

How can I make the equivalent in Objective-C elegant?

At first I, although this method would do the job:

- (void)getObjects:(id[])aBuffer range:(NSRange)aRange

But it copies the objects to the location of the buffer. I will need to add them back to another instance NSArray. In addition, the compiler complains about save / release properties that I'm really not comfortable with.

    id* objs;

    NSRange rng = NSMakeRange(1, [v1 count] - 1);

    [v1 getObjects:objs range:rng]; # Sending '__strong id *' 
                                    # to parameter of type '__unsafe_unretained id *'  
                                    # changes retain/release properties of pointer

So is there a more elegant way? Or is there no other way but to iterate through objectEnumerator?

+3
source share
1 answer
NSArray* v1 = @[@"a",@"b",@"c",@"d"];
NSRange rng = NSMakeRange(1,[v1 count]-1);
NSArray* v2 = [v1 subarrayWithRange:rng];
+10
source

All Articles