Skip to content

Instantly share code, notes, and snippets.

View johnhaney's full-sized avatar

John Haney johnhaney

View GitHub Profile
@johnhaney
johnhaney / ContentView.swift
Last active March 14, 2026 16:27
Calculate PI
//
// ContentView.swift
// CalculatePi
//
// Created by John Haney on 3/14/24.
//
import SwiftUI
import Combine
@johnhaney
johnhaney / ListOfLists.swift
Created May 1, 2022 16:15
ListOfLists crashes when adding an item (line 41)
import SwiftUI
struct Item: Identifiable {
let id = UUID()
var title: String
var details: String
}
struct ItemList: Identifiable {
let id = UUID()
@johnhaney
johnhaney / FiniteStateMachine.swift
Last active March 10, 2017 07:29
Protocol-Oriented StateMachine
protocol Transitionable
{
associatedtype State
associatedtype Transition
func nextState(from: State, via: Transition) -> State?
}
extension Transitionable
{
func canTransition(from: State, via: Transition) -> Bool
@johnhaney
johnhaney / UIBarButtonItem+Helper.swift
Last active August 14, 2016 06:33
Extension for simple creation of FlexibleSpace and FixedSpace UIBarButtonItems
// usage
toolbarItems = [.FlexibleSpace, doneButton, .FlexibleSpace]
extension UIBarButtonItem {
static var FlexibleSpace: UIBarButtonItem {
return SystemItem(.FlexibleSpace)
}
static var FixedSpace: UIBarButtonItem {
return SystemItem(.FixedSpace)
@johnhaney
johnhaney / gist:0608af5247ded67396c4
Last active November 5, 2015 17:18
Protocol extension on array, reworking of https://github.com/veasoftware/VSUniqueArray to apply directly to arrays instead of calling a method on a class.
extension Array where Element: Equatable {
var uniqueValues: Array<Element> {
get {
var collection = Array<Element>()
for element: Element in self
{
if !collection.contains(element)
{
collection.append(element)
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return fetchedResultsController.sections?.count ?? 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return fetchedResultsController.sections?[section].numberOfObjects ?? 0
}
@johnhaney
johnhaney / JHBlockObserver.m
Created May 8, 2015 22:54
Block-based solution for KVO
typedef void (^JHBlockObservedAction)(NSDictionary *change);
@interface JHBlockObserver : NSObject
@property (copy, nonatomic) JHBlockObservedAction block;
@property (strong, nonatomic) id object;
@property (strong, nonatomic) NSString *keyPath;
+ (JHBlockObserver *)observerForObject:(NSObject *)object forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options withBlock:(JHBlockObservedAction)block;