Skip to content

Instantly share code, notes, and snippets.

@vibrazy
Created February 21, 2012 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vibrazy/1877241 to your computer and use it in GitHub Desktop.
Save vibrazy/1877241 to your computer and use it in GitHub Desktop.
NSArray Category
//
// NSArray_NSArrayUtils.h
// GCDAndBlocks
//
// Created by Daniel Hollis on 21/02/2012.
// Copyright (c) 2012 Mobile Interactive Group. All rights reserved.
//
#import <Foundation/Foundation.h>
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
//ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
@interface NSArray (Utils)
-(id)safeObjectAtIndex:(NSUInteger)integer;
@end
@implementation NSArray (Utils)
-(id)safeObjectAtIndex:(NSUInteger)integer
{
if (__builtin_expect(([self count] > integer),1)) {
return [self objectAtIndex:integer];
} else {
NSArray *syms = [NSThread callStackSymbols];
if ([syms count] > 1) {
DLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:1]);
} else {
DLog(@"<%@ %p> %@", [self class], self, NSStringFromSelector(_cmd));
}
#ifdef DEBUG
@throw [NSException exceptionWithName:@"MIG: Index Out Of Bounds" reason:@"Array stack not big enough for index" userInfo:nil];
#endif
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment