Skip to content

Instantly share code, notes, and snippets.

@preble
preble / WeakSet.swift
Last active July 13, 2023 06:45
A pure Swift weak set.
//
// Created by Adam Preble on 2/19/15.
//
/// Weak, unordered collection of objects.
public struct WeakSet<T where T: AnyObject, T: Hashable> {
typealias Element = T
/// Maps Element hashValues to arrays of Entry objects.
/// Invalid Entry instances are culled as a side effect of add() and remove()
@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
#import "NSString+ConcurrentEnumeration.h"
@implementation NSString (ConcurrentLineEnumeration)
- (void)enumerateConcurrentlyWithOptions:(NSStringEnumerationOptions)options
usingBlock:(void (^)(NSString *substring))block
{
dispatch_group_t group = dispatch_group_create();
[self enumerateSubstringsInRange:NSMakeRange(0, [self length])
@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 / 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 / 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 / BNRBlockView.h
Created June 29, 2010 00:02
BNRBlockView: Add custom-drawn views to the Cocoa view hierarchy without subclassing.
@class BNRBlockView;
typedef void(^BNRBlockViewDrawer)(BNRBlockView *view, NSRect dirtyRect);
@interface BNRBlockView : NSView {
BNRBlockViewDrawer drawBlock;
BOOL opaque;
}
+ (BNRBlockView *)viewWithFrame:(NSRect)frame
@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 / DateRange.swift
Last active July 16, 2019 14:06
Experimenting with creating a SequenceType for iterating over a range of dates. Blogged here: http://adampreble.net/blog/2014/09/iterating-over-range-of-dates-swift/
import Foundation
func > (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == .OrderedDescending
}
extension NSCalendar {
func dateRange(startDate startDate: NSDate, endDate: NSDate, stepUnits: NSCalendarUnit, stepValue: Int) -> DateRange {
let dateRange = DateRange(calendar: self, startDate: startDate, endDate: endDate, stepUnits: stepUnits, stepValue: stepValue, multiplier: 0)
return dateRange

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