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
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) {}
});
@rnapier
rnapier / fix-xcode
Last active Aug 28, 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"
},
{
You can’t perform that action at this time.