Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice an NSArray in Objective-C?

What's the simplest way to extract a slice of an NSArray in Objective-C?

(much like the array_slice function in PHP)

like image 727
philfreo Avatar asked Nov 18 '09 01:11

philfreo


People also ask

How do you declare NSArray in Objective C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];

Can NSArray contain nil?

arrays can't contain nil.

How do you reverse an NSArray?

You can reverse a NSArray by writing your own loop iterating from the end towards the beginning and using a second array to add the items in reverse order. Or you can simply use - (NSEnumerator *)reverseObjectEnumerator from the NSArray class.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...


1 Answers

-[NSArray subarrayWithRange:] 

Reference:
https://developer.apple.com/documentation/foundation/nsarray/1415157-subarraywithrange

like image 134
Ole Begemann Avatar answered Sep 18 '22 14:09

Ole Begemann