View WeakSet.swift
1 2 3 4 5 6 7 8 9 10
//
// 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
1 2 3 4 5 6 7 8 9 10
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
1 2 3 4 5 6 7 8 9 10
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
1 2 3 4 5 6 7 8 9 10
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
1 2 3 4 5 6 7 8 9 10
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
1 2 3 4 5 6 7 8 9
NSArray *things = ^{
NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:numThings];
for (int i = 0; i < numThings; i++)
{
[tmpArray addObject: ... ];
}
return [tmpArray copy];
}();
 
View glerrcheck.h
1 2 3 4 5 6 7 8 9 10
// A macro I wrote a few years back to assist in checking OpenGL error codes.
// Add to a .h or at the top of your implementation file:
 
#define GLERR do {\
GLuint glerr;\
while((glerr = glGetError()) != GL_NO_ERROR)\
fprintf(stderr, "%s:%d glGetError() = 0x%04x", __FILE__, __LINE__, glerr);\
} while (0)
 
// Sprinkle in "GLERR;" after failure-prone OpenGL calls.
View RandomThreadSafetyTest.m
1 2 3 4 5 6 7 8 9 10
// Testing thread safety of rand() and random(); for more see:
// http://adampreble.net/blog/2012/09/mtrandom-an-objective-c-random-number-generator/
// Created by Adam Preble on 9/3/12
// Copyright (c) 2012 Adam Preble. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
#if 1
#define test_srandom srandom
Something went wrong with that request. Please try again.