Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

In iOS 7 NSArray have public property firstObject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment