365Cocoa

A snippet of Objective-C code per day, 365 days long.
Written by Pieter Omvlee, developer and owner of Bohemian Coding.

May 5

Day 47: Back from break: -tail

I’ve taken a little break for which I apologise; a combination of friends and family visiting and a short holiday. But we’re back.

I often find I need everything except the first item in an array. Instead of starting iterating a for loop at i=1, I wrote this:

- (NSArray *)tail
{
  if ([self count] == 0)
    return [NSArray array];

  return [self objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(1,[self count]-1)]];
}

  1. 365cocoa posted this