Skip to content

Instantly share code, notes, and snippets.

View plantpurecode's full-sized avatar
🎯

Jacob Relkin plantpurecode

🎯
View GitHub Profile
@interface UIView (JRHierarchyAdditions)
- (void)bringToFront;
- (void)sendToBack;
@end
@implementation UIView (JRHierarchyAdditions)
- (void)bringToFront {
[[self superview] bringSubviewToFront:self];
}
@plantpurecode
plantpurecode / gist:1340889
Created November 5, 2011 00:44
Birthday celebration
//GCD way:
double delayInSeconds = (24 * 60) * 60;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self setAge:[self age] + 1];
});
//NSInvocation way:
@plantpurecode
plantpurecode / NSIndexPath+JRAdditions.m
Created January 3, 2012 22:56
A handy category on NSIndexPath.
@implementation NSIndexPath (JRAdditions)
- (NSIndexPath *) indexPathByAddingRows:(NSInteger)rows andSections:(NSInteger)sections {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:MIN(NSIntegerMax, [self row] + rows)
inSection:MIN(NSIntegerMax, [self section] + sections)];
return newIndexPath;
}
- (NSIndexPath *) indexPathByAddingRows:(NSInteger) rows {
return [self indexPathByAddingRows:rows andSections:0];
@plantpurecode
plantpurecode / gist:1880242
Created February 22, 2012 00:45
Safe -objectAtIndex: method
@interface NSArray (JRAdditions)
- (id) safeObjectAtIndex:(NSUInteger)index;
@end
@implementation NSArray (JRAdditions)
- (id) safeObjectAtIndex:(NSUInteger)index {
@synchronized(self) {
@plantpurecode
plantpurecode / gist:1914668
Created February 26, 2012 07:13
Safe message invocation, untested
@interface JRObject : NSObject @end
@implementation JRObject
- (void)doesNotRecognizeSelector:(SEL)selector {
NSLog(@"Attempted to invoke %s on %@", selector, self);
}
@end
@interface NSArray(JRAdditions)
- (id)jr_objectAtIndex:(long long)index {
NSUInteger realIndex = index;
if(index < 0) {
realIndex = [self count] - (+index);
}
return [self objectAtIndex:realIndex];
}
@plantpurecode
plantpurecode / gist:3703384
Created September 12, 2012 00:57
Recursion, with blocks!
- (void)printRecursivelyUpTo:(NSUInteger)max {
__block dispatch_block_t block;
__block NSUInteger iterator = 0;
dispatch_block_t b = ^{
if(iterator == max) return;
NSLog(@"%u", iterator);
++iterator;
block();
};
@plantpurecode
plantpurecode / NSLock+JRAdditions.h
Created September 20, 2012 22:54
Locking with blocks
@interface NSLock (JRAdditions)
- (void)lockWithBlock:(dispatch_block_t)block;
@end
typedef void (^JRIterationBlock)(NSUInteger index);
static inline void invokeBlockIterativelyUsingRange(JRIterationBlock block, NSRange rng) {
NSParameterAssert(block);
for(NSUInteger i = rng.location; i < NSMaxRange(rng); ++i) {
block(i);
}
}
@implementation NSNumber (BlockAdditions)
@interface NSCountedSet (JRCountedSetAdditions)
- (NSArray *) objectsWithCount:(NSUInteger) count;
@end