Skip to content

Instantly share code, notes, and snippets.

@joshavant
joshavant / observer_pattern.md
Created April 17, 2014 22:04
Handy little design pattern that lets you kick off processes in arbitrary objects + lets arbitrary dependencies add themselves into a notification chain

##XXAppDelegate.m, XXSomeView.m, some other object, etc##

...
[[XXSomeSingletonController sharedInstance] doThatThing];
...

##XXSomeSingletonControllerObserver.h##

@protocol XXSomeSingletonControllerObserver 
@joshavant
joshavant / Foo.m
Created May 13, 2014 20:48
Thread-safe, forced initialization for singletons
#import "Foo.h"
static dispatch_once_t FooSharedDispatchOnceToken;
static Foo *FooSharedInstance;
+ (void)prepareService
{
[self fetchSharedServiceAndInitialize:YES];
}
@joshavant
joshavant / FOOClass.m
Created February 9, 2015 20:16
KVO Context Pattern
static void *kFOOClassKVOContext = &kFOOClassKVOContext;
@implementation FOOClass
- (void)addKVOObservers
{
[self.property addObserver:self forKeyPath:@"someKeyPath" options:0 context:kFOOClassKVOContext];
}
- (void)removeKVOObservers
@joshavant
joshavant / gist:1637633
Created January 19, 2012 03:41
My Open Source License: MIT Expat + Beerware
Copyright (c) <year> Josh Avant
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in

The recommended way to keep a long-term reference to a particular record is to store the first and last name, or a hash of the first and last name, in addition to the identifier. When you look up a record by ID, compare the record’s name to your stored name. If they don’t match, use the stored name to find the record, and store the new ID for the record.

-from Address Book Programming Guide for iOS, Using Record Identifiers, 2nd paragraph

@joshavant
joshavant / gist:3355530
Created August 15, 2012 03:45
Apple-safe UUID Generation
// requires UICKeyChainStore lib
NSString *uniqueIdentifier;
#if TARGET_IPHONE_SIMULATOR
uniqueIdentifier = @"SIMULATOR";
#else
uniqueIdentifier = [UICKeyChainStore stringForKey:@"UUID" service:@"joshavant"];
if(![uniqueIdentifier length] > 0)
func alertControllerToSelectContactMethod() -> UIAlertController {
let controller = UIAlertController(title: "How do you want to contact \(firstName.value)?", message: "", preferredStyle: .ActionSheet)
let methodsAndHandlers = contactMethodsAndHandlers()
if (methodsAndHandlers.count == 0) {
let action = UIAlertAction(title: NSLocalizedString("No Contact Info Found", comment: ""), style: .Default, handler: nil)
action.enabled = false
controller.addAction(action)
@joshavant
joshavant / scrollviews_insets_and_IB.md
Last active December 1, 2015 00:45
Translucent Bar Bliss: Scroll View Insetting w/ Nav + Tab Bars, in an IB Workflow

Translucent Bar Bliss: Scroll View Insetting w/ Nav + Tab Bars, in an IB Workflow

This document describes how to configure your scroll views in IB, so that their insets are configured to accomodate coverage by UINavigationBar and UITabBar.

A common reason for wanting this is to allow these views to configured as 'translucent', while scroll view content scrolls beneath them.

This guide is broken up into two types of view configurations...

  • Configuration 1: A top-level scroll view, which fills UIViewController's view
  • Configuration 2: A top-level scroll view, which does not fill UIViewController's view
import Foundation
protocol Pageable {
typealias PagedContentType
var pageLimit: Int { get set }
var pageOffset: Int { get set }
func getPage(limit limit: Int, offset: Int, completion: (result: [PagedContentType]?, error: NSError?) -> Void)
mutating func nextPage(completion: (result: [PagedContentType]?, error: NSError?) -> Void)
// Broken on 9.2, Worked on 9.1
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey]
// Works on 9.2 + 9.1
let keysToFetch = [CNContactPhoneNumbersKey, CNContactEmailAddressesKey, CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName)]