Skip to content

Instantly share code, notes, and snippets.

Rob Napier rnapier

View GitHub Profile
View decodeJSON.swift
// Based on
extension Bundle {
// This is synchronous, which is bad if called on the main queue.
func decodeJSON<T: Decodable>(_ type: T.Type = T.self, from filename: String) throws -> T {
guard let path = self.url(forResource: filename, withExtension: nil) else {
throw NSError(domain: NSCocoaErrorDomain,
code: CocoaError.fileNoSuchFile.rawValue,
userInfo: [NSFilePathErrorKey: filename])
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 / assertion.swift
Last active Nov 6, 2019
AssertionFailure as a type
View assertion.swift
/// Boring setup. See below for the good parts
public class Logger {
public static let root = Logger(subsystem: .none, parent: nil)
public let subsystem: Subsystem
public let parent: Logger?
public init(subsystem: Subsystem, parent: Logger? = .root) {
self.subsystem = subsystem
View JSONStringConvertible.swift
import Foundation
public protocol JSONStringConvertible: class {
var jsonString: String { get }
extension Logger {
// Converts an arbitrary object into some that is JSON-safe
static func makeJSONObject(_ object: Any) -> Any {
if let jsonObj = object as? JSONStringConvertible {
rnapier / TypeCoder.swift
Last active Jun 13, 2019
Decode based on a type field
View TypeCoder.swift
import Foundation
/// Simple example of decoding different values based on a "type" field
let json = Data("""
"type": "user",
"name": "Alice"
View APIClient.swift
import Foundation
// Provides a nice bare-init for ID types
protocol IDType: Codable, Hashable {
associatedtype Value
var value: Value { get }
init(value: Value)
extension IDType {
View DataStore.swift
import Foundation
protocol DataStorage {
subscript(identifier: String) -> Data? { get set }
extension UserDefaults: DataStorage {
subscript(identifier: String) -> Data? {
get { return data(forKey: identifier) }
set { set(newValue, forKey: identifier) }
View RepositoryStorage.swift
// Fully type-erased solution for
// I don't like type-erasers like this; I believe it can be sliced a better way.
// Compare, which I like, but relies on a common
// currency of serializing everything to Data and working with that instead of having storage that can deals with Element
// directly.
// This example is interesting because, like so many of these toy projects, its design is completely broken and wouldn't
rnapier / main.m
Last active Apr 5, 2019
GCD accessor
View main.m
// main.m
// test
// Created by Rob Napier on 4/5/19.
#import <Foundation/Foundation.h>
@interface MyClass: NSObject

Item #: SCP-10791

Object Class: Safe

Special Containment Procedures: SCP-10791 must be maintained within the BMP (Basic Multilingual Plane) at all times. Removal from the BMP may cause disaggregation of the component parts, increasing nearby values by 2. While this is not considered a memetic hazard, the accounting department has made it clear that they will not accept non-standard operators wandering the halls since Incident ██/██/███ when [REDACTED].

Description: SCP-10791 is the result of unauthorized experimentation with SCP-889, SCP-1313, and three members of the Unicode Technical Committee. The less that is said about that, the better. What happens several miles north of ███████, Montana, USA....sometimes requires containment.

Since ██/██/2002, SCP-10791 has been contained in the ███████ Mathematical Operators block. Its name and appearance change regularly, though always an unlikely combination of symbols. Critic

You can’t perform that action at this time.