Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A couple of extensions to NSArray that I use on a regular basis.
//
// NSArray+JCaddons.h
//
// Created by Jesse Crocker on 7/26/12.
// Copyright (c) 2012 Thisside.net. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSArray (JCaddons)
-(NSInteger)indexOfString:(NSString *)string;
-(NSArray *)reversedArray;
@end
@interface NSMutableArray (ShiftExtension)
-(id)shift;
-(id)pop;
- (NSMutableArray *)reversedArray;
@end
//
// NSArray+JCaddons.m
//
// Created by Jesse Crocker on 7/26/12.
// Copyright (c) 2012 Thisside.net. All rights reserved.
//
#import "NSArray+JCaddons.h"
@implementation NSArray (JCaddons)
-(NSInteger)indexOfString:(NSString *)string{
for(int i = 0; i < self.count; i++){
NSString *row = [self objectAtIndex:i];
if([row isEqualToString:string])
return i;
}
return -1;
}
- (NSArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end
@implementation NSMutableArray (ShiftExtension)
-(id)shift {
if([self count] < 1) return nil;
id obj = [self objectAtIndex:0];
[self removeObjectAtIndex:0];
return obj;
}
-(id)pop {
if([self count] < 1) return nil;
id obj = [self lastObject];
[self removeLastObject];
return obj;
}
- (NSMutableArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
NSEnumerator *enumerator = [self reverseObjectEnumerator];
for (id element in enumerator) {
[array addObject:element];
}
return array;
}
@end\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.