Skip to content

Instantly share code, notes, and snippets.

@bjhomer
bjhomer / gist:961065
Created May 8, 2011 03:17
Infinite recursion via NSLog
NSMutableArray *a = [NSMutableArray array];
[a addObject:a];
NSLog(@"a: %@", a);
@bjhomer
bjhomer / gist:1245520
Created September 27, 2011 16:19
Getting the current track in iTunes
#! /usr/local/bin/macruby
framework "ScriptingBridge"
iTunes = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")
iTunes.currentTrack.name
@bjhomer
bjhomer / gist:1650079
Created January 20, 2012 22:57
Waiting on asynchronous results without blocking the run loop
// This is a useful bit of code for waiting for the results of an
// asynchronous operation. It's especially useful in unit tests.
// It keeps the run loop going, so it's not blocking delegate callbacks.
BOOL waitForBlock(NSTimeInterval timeout,
BOOL (^condition)(void)) {
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
BOOL val = condition();
while ( val == NO && [timeoutDate timeIntervalSinceNow] > 0 ) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
@bjhomer
bjhomer / output
Created February 3, 2012 00:23
Blocks are copied often
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
__block int x;
NSLog(@"Address of x outside of block: %p", &x);
@bjhomer
bjhomer / UITableView+in_updateInBlocks.m
Created February 3, 2012 21:14
block-based tableview update API.
@implementation UITableView (in_updateInBlocks)
- (void)updateWithBlock:(void (^)(void))block {
[self beginUpdates];
block();
[self endUpdates];
}
@end
NSString *a = [NSString stringWithFormat:@"hi %@", @"there"];
NSMutableString *b = [NSMutableString stringWithString:@"hi"];
[b appendString:@" there"];
NSLog(@"[a isEqual:b]: %d", [a isEqual:b]); // 1
NSLog(@"[b isEqual:b]: %d", [b isEqual:a]); // 1
@bjhomer
bjhomer / UIWebView+AccessoryHiding.m
Created March 16, 2012 05:03
Hiding the inputAccessoryView of a UIWebView
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
@interface UIWebView (HackishAccessoryHiding)
@property (nonatomic, assign) BOOL hackishlyHidesInputAccessoryView;
@end
@implementation UIWebView (HackishAccessoryHiding)
static const char * const hackishFixClassName = "UIWebBrowserViewMinusAccessoryView";
@bjhomer
bjhomer / setFoo.m
Created March 21, 2012 21:04
A useful pattern for UIKit's -setFoo:animated: methods
- (void)setFoo:(BOOL)foo animated:(BOOL)animated {
[super setFoo:foo animated:animated];
dispatch_block_t changes = ^{
if (foo) {
someView.alpha = 1.0;
}
else {
someView.alpha = 0.0;
}
@bjhomer
bjhomer / CoreDataDeadlock.m
Created March 30, 2012 17:28
Deadlocking with a parent context
NSManagedObjectContext *backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
NSManagedObjectContext *mainQueueContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
mainQueueContext.parentContext = backgroundContext;
[backgroundContext performBlock:^{
id someManagedObject = [backgroundContext objectWithID:someID];
// Ensure that the object is not a fault
@bjhomer
bjhomer / 0_explanation.md
Created April 23, 2012 19:32
Putting "*.m diff=objc" in <repo_root>/.gitattributes

Running echo "*.m diff=objc" >> .gitattributes in a git repo tells git to use the obj-c diff algorithm when running git diff. For the most part, this isn't much different from the standard diff algorithm. However, it adds one key benefit.

In a unified git diff, added lines are prefixed with a +, and removed lines are prefixed with a -. Unchanged lines are provided for context, and have no prefix. As added context, though, unified diffs have a @@-prefixed line at the beginning of each hunk. Minimally, the @@ lines specify which lines in the file are being changed. It can also contain a label, if git can determine an appropriate label for that section of code.

By default, the label on a hunk context line is the name of the enclosing C function. That works great for C-ish code, but completely misses Obj-C method signatures. As a result, when diffing Obj-C files, the context label is either empty, or falls back to the last C-ish declaration it could find. This is usually entirely useless.

Adding