Skip to content

Instantly share code, notes, and snippets.

Rob Napier rnapier

Block or report user

Report or block rnapier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@rnapier
rnapier / json.swift
Last active Aug 12, 2019
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)"
} else {
@rnapier
rnapier / fix-xcode
Last active Aug 8, 2019
Links Xcode SDKs from the /SDKs directory (which you maintain yourself)
View fix-xcode
#!/usr/bin/python
# fix-xcode
# Rob Napier <robnapier@gmail.com>
# 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:
View TitleDecodable.swift
// From https://stackoverflow.com/questions/54129682/use-swift-codable-to-decode-json-with-values-as-keys
import Foundation
let json = Data("""
{
"7E7-M001" : {
"Drawer1" : {
"101" : {
"Partnumber" : "F101"
},
@rnapier
rnapier / stream.swift
Last active Jun 22, 2019
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
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 {
dispose()
}
}
@rnapier
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 RepositoryStorage.swift
// Fully type-erased solution for
// https://stackoverflow.com/questions/55549318/avoiding-type-erasure-when-implementing-the-repository-pattern/55550454
// I don't like type-erasers like this; I believe it can be sliced a better way.
// Compare https://gist.github.com/rnapier/f7f0fa6202b0d6586af188635f54b28b, 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.
// FURTHER THOUGHTS:
//
// This example is interesting because, like so many of these toy projects, its design is completely broken and wouldn't
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) }
@rnapier
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
You can’t perform that action at this time.