Skip to content

Instantly share code, notes, and snippets.

protocol Protocol {
func methodInProtocol() -> String
}
extension Protocol {
func methodInProtocol() -> String {
return "Protocol"
}
func methodOnExtension() -> String {
@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) {
@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.

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 / 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)"
}
}
@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
@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 / glerrcheck.h
Created October 24, 2012 14:04
glGetError() test macro
// 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.
@preble
preble / RandomThreadSafetyTest.m
Created September 3, 2012 15:16
Test thread safety of random number generators.
// 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