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 {
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"
},
View decodeJSON.swift
// Based on https://twitter.com/ravibastola/status/1249555595285291008?s=20
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])
}
@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 / doOnce.swift
Last active Feb 19, 2020
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 = {
View erasure.swift
import Swift
/*:
A simple type-erased sequence
*/
let seq = AnySequence([1,2,3])
/*:
## Who Needs Types Like That?
@rnapier
rnapier / json.swift
Last active Nov 8, 2019
Generic JSON Decodable
View json.swift
import Foundation
@dynamicMemberLookup
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)"
@rnapier
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
rnapier / gist:7515273
Created Nov 17, 2013
Exploration of thread priorities vs queue priorities
View gist:7515273
// Main thread priorty = 0.758065
NSLog(@"main:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
// High priority = 0.532258
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(@"high:%@:%f", [NSThread currentThread], [NSThread threadPriority]);
// run forever! Note that other queues still happily process, even though our high-priority block never ends
while(1) {}
});
You can’t perform that action at this time.