Skip to content

Instantly share code, notes, and snippets.


Rob Napier rnapier

View GitHub Profile
View StyledText.swift
// TextStyle.swift
// Created by Rob Napier on 12/20/19.
// Copyright © 2019 Rob Napier. All rights reserved.
import SwiftUI
public struct TextStyle {
rnapier / doOnce.swift
Last active Apr 20, 2022
A function that only executes once. Good Swift or over-clever?
View doOnce.swift
// Swift3 gets rid of dispatch_once and recommends replacing it with a lazy global.
// That's very straightforward when dispach_once is used to initialize something, but
// isn't an exact match when you want something to execute once, and then become a noop
// in a thread-safe way.
// The following approach seems completely "correct" and I guess actually a bit elegant,
// if by "elegant" you mean "terse and not immediately obvious to the reader, which makes
// you look very clever."
var doOnce: () -> Void = {
rnapier / observer.swift
Last active Apr 17, 2022
New ideas on Observer pattern in Swift
View observer.swift
import UIKit
// Lots of ideas here that I'd love thoughts about. Some of this creates new highly generic types
// (UniqueIdentifier, IdentifiedSet) that may have broader purpose. Some of it defines a new Observer pattern.
// I've been playing around with this UniqueIdentifier type. Its purpose is to let you store things
// in dictionaries (or possibly sets) that you couldn't otherwise.
// It's just a self-referencial ObjectIdentifier. I used to use NSUUID for this purpose, but wondering
// if this is better.
rnapier / fix-xcode
Last active Mar 18, 2022
Links Xcode SDKs from the /SDKs directory (which you maintain yourself)
View fix-xcode
# fix-xcode
# Rob Napier <>
# Script to link in all your old SDKs every time you upgrade Xcode
# Create a directory called /SDKs (or modify source_path).
# Under it, put all the platform directories:
# MacOSX.platform iPhoneOS.platform iPhoneSimulator.platform
# Under those, store the SDKs:
rnapier / json.swift
Last active Oct 7, 2021
Generic JSON Decodable
View json.swift
import Foundation
enum JSON: Codable, CustomStringConvertible {
var description: String {
switch self {
case .string(let string): return "\"\(string)\""
case .number(let double):
if let int = Int(exactly: double) {
return "\(int)"
View ValueStream.swift
import Foundation
public class Disposable {
private var isDisposed = false
private let _dispose: () -> Void
public func dispose() {
if !isDisposed {
isDisposed = true
rnapier / stream.swift
Last active Jun 3, 2021
Newer stream-based ideas on observables
View stream.swift
Updated ideas on observation. Much more powerful and composeable than previous Observable approach.
Simpler, but less powerful, than RxSwift
import Foundation
public class Disposable {
private var isDisposed = false
private let _dispose: () -> Void
rnapier / observable.swift
Created May 16, 2018
New Observable idea
View observable.swift
import Foundation
class Disposable {
let dispose: () -> Void
init(dispose: @escaping () -> Void) { self.dispose = dispose }
deinit {
rnapier / GridView.swift
Last active Sep 27, 2020
GridView that makes me cry
View GridView.swift
// This GridView makes me cry. It is recreating an HTML-style bordered table, sized to
// its data, with a header. It requires a GeometryReader and Preferences, which might
// be unavoidable, but it also requires a *horrible* DispatchQueue.main.async in updateMaxValue.
// This means it doesn't work in Previews, and completely breaks the idea of "declarative" UI.
import SwiftUI
struct WidthPreferenceKey: PreferenceKey {
static var defaultValue: CGFloat = .zero
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
View erasure.swift
import Swift
A simple type-erased sequence
let seq = AnySequence([1,2,3])
## Who Needs Types Like That?