Skip to content

Instantly share code, notes, and snippets.

// 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)]
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)
@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
@joshavant
joshavant / NibLoadable.swift
Created October 9, 2015 21:08
NibLoadableView
import UIKit
protocol NibLoadable {
func setupFromXib()
}
// This extension loads a .xib file with the same filename as the class name
// i.e. `class FoobarHeaderView` -> loads FoobarHeaderView.xib
extension NibLoadable where Self: UIView {
func setupFromXib() {
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)

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 / 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 / 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 / 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 / privacy.md
Created August 9, 2013 00:22
Current HoR Members Supporting Privacy

###Current members of the House of Representatives who voted: NO - PATRIOT Act
NO - 2005 Reauthorization of PATRIOT Act
NO - 2006 Reauthorization of PATRIOT Act
NO - 2011 Reauthorization of PATRIOT Act
YES - Amash Amendment

  • Rep. Michael Capuano [D-MA7]
  • Rep. Elijah Cummings [D-MD7]
  • Rep. Danny Davis [D-IL7]