Skip to content

Instantly share code, notes, and snippets.

View MartinJNash's full-sized avatar

Martin Nash MartinJNash

View GitHub Profile
@MartinJNash
MartinJNash / gist:e2bb27937711f350aa44
Created June 7, 2014 00:43
Class name to string in Swift
class Person {
class func classString() -> String {
return NSStringFromClass(self)
}
}
Person.classString() // "_TtC11lldb_expr_06Person"
@MartinJNash
MartinJNash / CustomNSViewIBDesignable.swift
Last active May 2, 2018 21:32
Xcode Snippets - NSView & UIView IBDesignable
import Cocoa
@IBDesignable
class <#ClassName#> : NSView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
commonSetup()
}
@MartinJNash
MartinJNash / SwiftRubyCrowbarOperator.swift
Last active December 14, 2015 17:30
Ruby Crowbar Operator in Swift
import Cocoa
infix operator ||= {}
func ||= <T> (inout value: T?, newValue: @autoclosure () -> T) -> T {
if value == nil {
value = newValue()
}
return value!
@MartinJNash
MartinJNash / FirstTimeOrAnother.swift
Last active August 29, 2015 14:23
A Swift class to dispatch blocks either the first time and or all but the first time.
import Foundation
public final class FirstOrAllOtherDispatcher {
private var firstTimeOnlyBlock: (()->Void)?
private var allOtherTimesBlock: (()->Void)?
public init(firstTime: ()->Void) {
self.firstTimeOnlyBlock = firstTime
self.allOtherTimesBlock = nil
func randBetween(range: Range<Int>) -> Int {
let num = arc4random_uniform(UInt32(range.endIndex + 1))
return Int(num) + range.startIndex
}
extension Array {
var shuffled: Array<T> {
var newer = self
newer.shuffle()
return newer
}
mutating func shuffle() {
@MartinJNash
MartinJNash / NoMoveWindow.swift
Created March 27, 2015 17:59
Hide NSDocument's "move to" titlebar button
import AppKit
class NoMoveWindow: NSWindow {
override class func standardWindowButton(b: NSWindowButton, forStyleMask m: Int) -> NSButton? {
if b == NSWindowButton.DocumentVersionsButton {
return nil
}
@MartinJNash
MartinJNash / Dictionary+Extensions.swift
Created January 12, 2015 16:33
Functional methods for Swift dictionaries
extension Dictionary {
func map<T>(tx: Element->T) -> [T] {
var memory: [T] = []
for (k, v) in self {
memory.append(tx(k, v))
}
return memory
}
@MartinJNash
MartinJNash / Array+Repetition.swift
Created December 30, 2014 03:48
Repetition of array elements
extension Array {
/// [1, 2, 3] by 2 produces: [1, 1, 2, 2, 3, 3]
func replicated(times: UInt) -> Array {
return self.reduce([], combine: { mem, curr in
mem + Array(count: Int(times), repeatedValue: curr)
})
}
@MartinJNash
MartinJNash / NSFetchedResultsController+LinearCollection.swift
Last active August 29, 2015 14:09
Treat a sectioned NSFetchedResults controller as a flat or linear collection.
import CoreData
extension NSFetchedResultsController {
var indexPaths: [NSIndexPath] {
var paths = [NSIndexPath]()
var sectionNumber = 0
for singleSection in sections as [NSFetchedResultsSectionInfo] {