Skip to content

Instantly share code, notes, and snippets.

@preble
preble / gist:5306137
Last active December 15, 2015 18:48
Using Objective-C blocks for encapsulation while preparing a local variable.
NSArray *things = ^{
NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:numThings];
for (int i = 0; i < numThings; i++)
{
[tmpArray addObject: ... ];
}
return [tmpArray copy];
}();
@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)"
}
}

Objective-C Bracing Style

I'm Adam Preble and this is the bracing style I like. Please don't use it unless you (a) like it, or (b) are contributing to a project of mine.

Rules

  • Braces go on the next line for:
    • Method definitions.
    • if blocks:
    • After case statements, but only if required by local variables within the case.
@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) {
protocol Protocol {
func methodInProtocol() -> String
}
extension Protocol {
func methodInProtocol() -> String {
return "Protocol"
}
func methodOnExtension() -> String {

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

@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
@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 / 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