Skip to content

Instantly share code, notes, and snippets.

View bentford's full-sized avatar

Ben Ford bentford

View GitHub Profile
@bentford
bentford / gist:4549876
Last active December 11, 2015 05:08
CoreData: merging changes from background context.
// getting this error?
// "An observer of NSManagedObjectContextDidSaveNotification illegally threw an exception"
// observing the notification here
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(otherContextDidSave:)
name:NSManagedObjectContextDidSaveNotification object:nil];
// FIX: you must mergeChangesFromContextDidSaveNotification: on main thread
likes(bear, honey).
likes(human, honey).
likes(racoon, chicken).
likes(pig, slop).
likes(vulture,roadkill).
flavor(sweet, honey).
flavor(savory, chicken).
flavor(gross, slop).
flavor(gross,roadkill).
@bentford
bentford / gist:5558108
Created May 10, 2013 23:07
The easiest way to implement NSFastEnumeration is to build your custom collection on top of NSFoundation collections.
#pragma mark - NSFastEnumeration
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
objects:(__unsafe_unretained id [])buffer count:(NSUInteger)len
{
return [dictionary.allValues countByEnumeratingWithState:state objects:stackbuf count:len];
}
#pragma mark -
@bentford
bentford / AutoLayoutLabel
Created June 17, 2013 20:49
Shows how to implement a multiline UILabel with Auto Layout. It will dynamically resize to fit whatever horizontal constraints you apply.
@interface AutoLayoutLabel : UILabel
@end
@implementation AutoLayoutLabel
{
CGFloat overrideWidth;
}
- (id)init
//
// CustomNavViewController.h
//
//
// Created by Ben Ford on 11/28/12.
// Copyright (c) 2012 Ben Ford. All rights reserved.
//
#import <UIKit/UIKit.h>
@bentford
bentford / gist:7791449
Last active December 30, 2015 06:48
Showing and hiding the keyboard on iOS
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
@bentford
bentford / StyleUtil.m
Last active August 29, 2015 14:00
Example of a iOS Stylesheet
//
// StyleUtil.m
#import "StyleUtil.h"
// Normally I'll import UIColor+Hex.h
// but for sake of the gist, I included the category inline
// #import "UIColor+Hex.h"