View erasure.swift
import Swift | |
/*: | |
A simple type-erased sequence | |
*/ | |
let seq = AnySequence([1,2,3]) | |
/*: | |
## Who Needs Types Like That? |
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)" |
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 { |
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) {} | |
}); |
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" | |
}, |
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 |
View observable.swift
import Foundation | |
class Disposable { | |
let dispose: () -> Void | |
init(dispose: @escaping () -> Void) { self.dispose = dispose } | |
deinit { | |
dispose() | |
} | |
} |
View TypeCoder.swift
import Foundation | |
/// Simple example of decoding different values based on a "type" field | |
let json = Data(""" | |
[{ | |
"type": "user", | |
"name": "Alice" | |
}, | |
{ |
NewerOlder