Skip to content

Instantly share code, notes, and snippets.

@DwightChan
Forked from aegzorz/NSArray+FirstObject.h
Created October 14, 2016 12:35
Show Gist options
  • Select an option

  • Save DwightChan/dbf3a239bae1156f630503c0144e742a to your computer and use it in GitHub Desktop.

Select an option

Save DwightChan/dbf3a239bae1156f630503c0144e742a to your computer and use it in GitHub Desktop.
Gets the first object in an array or returns nil for empty array.
//
// NSArray+FirstObject.h
//
#import <Foundation/Foundation.h>
@interface NSArray (FirstObject)
- (id)firstObject;
@end
//
// NSArray+FirstObject.m
//
#import "NSArray+FirstObject.h"
@implementation NSArray (FirstObject)
- (id)firstObject
{
return self.count > 0 ? self[ 0 ] : nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment