Skip to content

Instantly share code, notes, and snippets.


Adam Preble preble

View GitHub Profile
preble / AssertUnwrap.swift
Created Sep 16, 2020
This is Nate's idea, something I use on every project. Sometimes you need to accommodate an optional that shouldn't be optional.
View AssertUnwrap.swift
public extension Optional {
/// Stop in the debugger in debug builds if self is `.none`.
/// Example usage:
/// guard let value = maybeValue.assertUnwrap() else { return "bogus value" }
func assertUnwrap(_ message: @autoclosure () -> String? = nil, file: StaticString = #file, function: String = #function, line: UInt = #line) -> Wrapped? {
switch self {
preble / autostart
Created Aug 15, 2018
Autostart JMRI PanelPro on a Raspberry Pi -- without launching two instances.
View autostart
# Full path: /home/pi/.config/lxsession/LXDE-pi/autostart
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xset s off
@xset -dpms
@xset s noblank

Was getting the following errors when calling AVAssetImageGenerator generateCGImagesAsynchronously. The stream in question was an HLS stream. When I switched to using the AVURLAsset of an MP4, the error disappeared.

With Apple-supplied HLS stream:

Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo={NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action, NSUnderlyingError=0x1c0057850 {Error Domain=NSOSStatusErrorDomain Code=-12178 "(null)"}}

With another HLS stream generated by a video hosting provider:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12436), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1c4a57e20 {Error Domain=NSOSStatusErrorDomain Code=-12436 "(null)"}}

View ProtocolExtensions.swift
protocol Protocol {
func methodInProtocol() -> String
extension Protocol {
func methodInProtocol() -> String {
return "Protocol"
func methodOnExtension() -> String {
preble / Mutated.swift
Created Apr 11, 2016
Need to mutate a value type but don't want to create a var in your scope?
View Mutated.swift
protocol Mutatable {}
extension Mutatable {
func mutated(f: (inout Self) -> Void) -> Self {
var copy = self
return copy
preble / NSTextStorageSubclass.swift
Created Feb 9, 2016
Base subclass of NSTextStorage in Swift
View NSTextStorageSubclass.swift
import Cocoa
class SomeTextStorage: NSTextStorage {
private var storage: NSMutableAttributedString
override init() {
storage = NSMutableAttributedString(string: "", attributes: nil)
preble / Completion.swift
Created Jan 20, 2016
A cancellable completion for Swift.
View Completion.swift
final class Completion<R> {
private let closure: (R) -> Void
private var cancelled = false
/// `closure` is called upon completion, if not cancelled.
init(closure: (R) -> Void) {
self.closure = closure
preble / PivotalTrackerMarkdownLink.js
Created Jan 20, 2016
Want an OS X Service to convert a Pivotal Tracker story id or URL into a Markdown link? Create a new Service in Automator and enter the following in a Run JavaScript step. Save, give it a name, and you'll be able to select it from the Services menu.
View PivotalTrackerMarkdownLink.js
View KeyValueObserver.swift
import Foundation
/// Helper object for more Swift-like KVO. To create, call NSObject.addObserverForKeyPath(_:handler:).
class KeyValueObserver: NSObject {
private weak var observing: NSObject?
private var keyPath: String
private var handler: () -> Void
private init(observing: NSObject, keyPath: String, handler: () -> Void) {
preble / ErrorType+LocalizedDescription.swift
Created Dec 16, 2015
Adds a sort of localizedDescription to ErrorType.
View ErrorType+LocalizedDescription.swift
extension ErrorType {
var myLocalizedDescription: String {
if self.dynamicType == NSError.self {
return ((self as Any) as! NSError).localizedDescription
else {
return "\(self)"
You can’t perform that action at this time.