Skip to content

Instantly share code, notes, and snippets.

@preble
preble / AssertUnwrap.swift
Created September 16, 2020 23:55
This is Nate's idea, something I use on every project. Sometimes you need to accommodate an optional that shouldn't be optional.
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
preble / autostart
Created August 15, 2018 01:53
Autostart JMRI PanelPro on a Raspberry Pi -- without launching two instances.
# 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
@/home/pi/jmri-runner.sh

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)"}}

protocol Protocol {
func methodInProtocol() -> String
}
extension Protocol {
func methodInProtocol() -> String {
return "Protocol"
}
func methodOnExtension() -> String {
@preble
preble / Mutated.swift
Created April 11, 2016 18:23
Need to mutate a value type but don't want to create a var in your scope?
protocol Mutatable {}
extension Mutatable {
func mutated(f: (inout Self) -> Void) -> Self {
var copy = self
f(&copy)
return copy
}
}
@preble
preble / NSTextStorageSubclass.swift
Created February 9, 2016 04:45
Base subclass of NSTextStorage in Swift
import Cocoa
@objc
class SomeTextStorage: NSTextStorage {
private var storage: NSMutableAttributedString
override init() {
storage = NSMutableAttributedString(string: "", attributes: nil)
super.init()
@preble
preble / Completion.swift
Created January 20, 2016 23:32
A cancellable completion for 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
preble / PivotalTrackerMarkdownLink.js
Created January 20, 2016 03:01
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.
@preble
preble / KeyValueObserver.swift
Created January 7, 2016 18:59
Swift-like KVO
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
preble / ErrorType+LocalizedDescription.swift
Created December 16, 2015 20:30
Adds a sort of localizedDescription to ErrorType.
extension ErrorType {
var myLocalizedDescription: String {
if self.dynamicType == NSError.self {
return ((self as Any) as! NSError).localizedDescription
}
else {
return "\(self)"
}
}