Skip to content

Instantly share code, notes, and snippets.

@tomohisa
Created July 8, 2012 06:53
Show Gist options
  • Save tomohisa/3069721 to your computer and use it in GitHub Desktop.
Save tomohisa/3069721 to your computer and use it in GitHub Desktop.
Using ObjectAtIndex safely with this method.
//
// NSArray+IndexHelper.h
// C_POS
//
// Created by Tomohisa Takaoka on 6/14/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSArray (IndexHelper)
-(id) safeObjectAtIndex:(NSUInteger)index;
@end
//
// NSArray+IndexHelper.m
// C_POS
//
// Created by Tomohisa Takaoka on 6/14/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "NSArray+IndexHelper.h"
@implementation NSArray (IndexHelper)
-(id) safeObjectAtIndex:(NSUInteger)index {
if (index>=self.count) {
return nil;
}
return [self objectAtIndex:index];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment