Skip to content
View DispatchSyncThrows.swift
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
View demo_foreign.c
#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);
}
View WeakSet.swift
//
// 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()
View IdentifierGenerator.swift
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]
View gist:b819c22f6de4d68a5606
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,
View DateRange.swift
func < (left: NSDate, right: NSDate) -> Bool {
return left.compare(right) == NSComparisonResult.OrderedAscending
}
struct DateRange : SequenceType {
var calendar: NSCalendar
var startDate: NSDate
var endDate: NSDate
var stepUnits: NSCalendarUnit
View RandomAppDelegate.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
}
View objc-bracing.md

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.
View gist:5306137
NSArray *things = ^{
NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:numThings];
for (int i = 0; i < numThings; i++)
{
[tmpArray addObject: ... ];
}
return [tmpArray copy];
}();
View gist:5117339
Something went wrong with that request. Please try again.