A couple of extensions to NSArray that I use on a regular basis.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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