Skip to content

Instantly share code, notes, and snippets.

@mundstein
mundstein / NSObject+Block.m
Created April 22, 2012 19:19
Extend NSObject to perform block after delay
@implementation NSObject (PerformBlockAfterDelay)
- (void)performBlock:(void (^)(void))block
afterDelay:(NSTimeInterval)delay
{
block = [[block copy] autorelease];
[self performSelector:@selector(fireBlockAfterDelay:)
withObject:block
afterDelay:delay];
}
@mundstein
mundstein / Date.m
Created March 18, 2012 14:43
Objective-C method to return last or next Monday from date.
-(NSDate *) lastMondayBeforeDate:(NSDate*)timeStamp {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps =
[gregorian components:NSWeekdayCalendarUnit fromDate:timeStamp];
NSInteger weekday = [comps weekday];
weekday = weekday==1 ? 6 : weekday-2; // start with 0 on Monday rather than 1 on Sunday
NSTimeInterval secondsSinceMondayMidnight =
(NSUInteger) [timeStamp timeIntervalSince1970] % 60*60*24 +
weekday * 60*60*24;
return [timeStamp dateByAddingTimeInterval:-secondsSinceMondayMidnight];
@mundstein
mundstein / CustomView.m
Created February 12, 2012 11:17
Log the frame changes of a custom view
// override this method to get a great log output
// to show how the view is resized when rotating,
// calling sizeToFit and other methods that affect the frame
- (void) setFrame:(CGRect)frame {
[super setFrame:frame];
NSLog(@"%@ - %@", NSStringFromSelector(_cmd), NSStringFromCGRect(frame));
}
@mundstein
mundstein / TableViewController.m
Created December 22, 2011 14:01
Horizontal Tableview e.g. Image browser with minimal code
// Make a picture browser, just like in the Photo app
// **With only six additional lines of code!**
// in viewDidLoad:
self.tableView.transform = CGAffineTransformMakeRotation(-M_PI/2); // turn the table 90° anti-clockwise
self.tableView.showsVerticalScrollIndicator = NO;
self.tableView.pagingEnabled = YES;
@mundstein
mundstein / AppDelegate.m
Created December 22, 2011 08:41
Cool objective-c code spy
// Override this method in any class to get a pretty list of
// all methods that are being called - very useful.
// Try it on the AppDelegate for example
-(BOOL)respondsToSelector:(SEL)aSelector {
printf("The spy says: %s\n", [NSStringFromSelector(aSelector) UTF8String]);
return [super respondsToSelector:(aSelector)];
}