Skip to content

Instantly share code, notes, and snippets.

@preble
preble / DispatchSyncThrows.swift
Created June 27, 2015 00:25
An overload of dispatch_sync that throws any error thrown by the closure. (Swift 2)
import Foundation
/// Call a throwing block synchronously.
func dispatch_sync(queue: dispatch_queue_t, block: () throws -> ()) throws {
var error: ErrorType?
dispatch_sync(queue) {
do {
try block()
} catch let caughtError {
error = caughtError
@preble
preble / demo_foreign.c
Last active August 29, 2015 14:21
Demonstration of calling a Wren method from the current embedding API. More on Wren: http://wren.io/ Derived from: https://github.com/munificent/wren/issues/194
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "wren.h"
void logFromWren(WrenVM *vm) {
//double x = wrenGetArgumentDouble(vm, 0); // returns 0 in this setting
const char *message = wrenGetArgumentString(vm, 1);
printf("logFromWren: %s\n", message);
}
@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 / IdentifierGenerator.swift
Created February 7, 2015 14:30
Generate random identifiers in the style of Xcode.
public class IdentifierGenerator {
private let characters = Array("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
private let separator = "-"
public func generateIdentifier() -> String {
let numCharacters = UInt32(characters.count)
func randomCharacter() -> Character {
let index = Int(arc4random_uniform(numCharacters))
return characters[index]
@preble
preble / gist:b819c22f6de4d68a5606
Created November 7, 2014 16:22
NSView extension: insetSubview(_:dx:dy:)
extension NSView {
func insetSubview(subview: NSView, dx: CGFloat, dy: CGFloat) {
addSubview(subview)
subview.translatesAutoresizingMaskIntoConstraints = false
let constrain = { (attribute: NSLayoutAttribute, constant: CGFloat) in
subview.addConstraint(NSLayoutConstraint(
item: subview,
attribute: attribute,
relatedBy: .Equal,
@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 / RandomAppDelegate.swift
Last active August 29, 2015 14:02
Random app in Swift
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var window: NSWindow
@IBOutlet var label: NSTextField
func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
}

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 / 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 / gist:5117339
Last active December 14, 2015 16:48
iOS & Cocoa Resources