Skip to content

Instantly share code, notes, and snippets.

@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

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

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 / 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() {
@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)]
@joshavant
joshavant / custom_colors.md
Last active January 30, 2016 18:36
How to manage custom OS X color palettes in your Xcode project

How to manage custom OS X color palettes in your Xcode project

The following steps configure your Xcode project to check the status of your custom color palette in the system, during the build process.

When your custom color palette is not installed in the system, Xcode's build process will fail with an error, instructing you to run a script which will install the file.

When the installed color palette doesn't match what is in your project's root directory (according to an md5 file checksum comparison), Xcode's build process will warn you to update the .clr file.

This system allows you to keep your custom color palette file in your project directory, under source control. Whenever changes to the palette file are pushed to the repo, Xcode will notice them and notify you during the build process.

@joshavant
joshavant / KeyboardMetricsService.swift
Last active March 11, 2016 21:47
A basic, thin wrapper on keyboard appearance handling. This is mostly just a starting point: this only updates its `var currentMetrics`, which leaves to the user the exercise of adding whatever update hooks make sense for their codebase (likely through subclassing).
//
// KeyboardMetricsService.swift
// CrowdRise
//
// Created by Josh Avant on 3/10/16.
// Copyright © 2016 CrowdRise. All rights reserved.
//
import UIKit
import UIKit
func namedModels() -> [ModelObject] {
let names: [String] = UIFont.familyNames()
return names.map(ModelObject.init)
}
class ModelObject: NSObject {
let name: String
init(name: String) { self.name = name }