Managing the iOS Network Activity Indicator

The network activity indicator in iOS is the spinning wheel in the upper left of the screen in the status bar next to the carrier name and connection strength meters. The indicator spins when the device is using the network offering the user great feedback when they are using data. more…

Get First n Items of NSArray in Objective C, or any range.

If you need to get the first n items in an NSArray in Objective C then you can use the subarrayWithRange: method. This method will work to get any range of items of an array and will return a new array containing those items. For example, to get the first 20 elements of the NSArray myArray, try this:

NSArray *firstTwentyItems = [myArray subArrayWithRange:NSMakeRange(0, MIN(20, [myArray count])];

To get the last 20 elements simply update the range like this:

NSArray *lastTwentyItems = [myArray subArrayWithRange:NSMakeRange(MAX(0, ([myArray count] - 20)), [myArray count])];

Using the MIN() and MAX() macros help ensure the range is within the bounds of the NSArray.