Skip to content

Instantly share code, notes, and snippets.

Avatar

Brian King KingOfBrian

  • Boston Massachusetts
View GitHub Profile
View gist:776887f4876269402351
import Foundation
class Foo {
func bar() {
println("Swift Hello, World!")
}
@objc
func baz() {
println("baz")
}
@KingOfBrian
KingOfBrian / xcassetgen.swift
Last active Aug 29, 2015
Code Generation of .xcasset resources
View xcassetgen.swift
import Foundation
/**
* Build script to generate a type safe wrapper around your projects .xcasset file.
* This will fail your build if you reference an image in your .xcasset file that has
* changed or been removed, as well as provide code completion help for all your images.
*
* Copy this file into a new `Run Phase` in your project, with `/usr/bin/env xcrun swift`
* specified for `Shell`.
*
* Configure the variables below:
View NSObject+BlockObservation.h
//
// NSObject+BlockObservation.h
// Version 1.0
//
// Andy Matuschak
// andy@andymatuschak.org
// Public domain because I love you. Let me know how you use it.
//
#import <Cocoa/Cocoa.h>
View gist:910457
typedef enum {
BKSlideDirectionRight = 1 << 0,
BKSlideDirectionLeft = 1 << 1,
BKSlideDirectionUp = 1 << 2,
BKSlideDirectionDown = 1 << 3,
BKSlideDirectionVertical = BKSlideDirectionUp | BKSlideDirectionDown,
BKSlideDirectionHorizontal = BKSlideDirectionLeft | BKSlideDirectionRight
} BKSlideDirection;
View gist:8d2c6d85cb4079aabde6
import Foundation
operator infix =~ {}
func =~ (input: String, pattern: String) -> String[]? {
let regex = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: nil)
let results = regex.matchesInString(input,
options: nil,
range: NSMakeRange(0, countElements(input))
View wat2.swift
let strA: String? = check ? "hrm" : nil
print(strA) // .None
let str: String = "hrm"
let strB: String? = check ? str : nil
print(strB) // .Some("Awesome Idea.")
@KingOfBrian
KingOfBrian / is_this_interesting.noidea
Last active Sep 28, 2016
List of last verb * -> noun punctuation from Debate
View is_this_interesting.noidea
welcome you to the first presidential debate
sponsored by the Commission on Presidential Debates
drafted tonight's format
agreed to by the campaigns
divided into six segments
explore three topic areas tonight
have an open discussion
shared with the commission or the campaigns
we welcome the candidates
cover all the issues of this campaign tonight
@KingOfBrian
KingOfBrian / gist:9739d45c20b6bc8daea4a288c3e35bef
Created Dec 5, 2016
Specialization incorrectly uses the default implementation
View gist:9739d45c20b6bc8daea4a288c3e35bef
protocol Factory {
associatedtype Value = Self
static func make() -> Value
}
extension Factory {
static func make() -> Value {
fatalError("In default static implementation")
}
}
View Computr.swift
import Foundation
struct Computr {
typealias Integer = Int
typealias Address = UnsafeMutablePointer<Integer>
var ic: Address = Address.allocate(capacity: 1)
var eax: Integer = 0
var memory = Address.allocate(capacity: 10)
mutating func addr(at index: Int) -> Address {
@KingOfBrian
KingOfBrian / XCTestAndNil.md
Last active Feb 14, 2017
XCTest and nil assertions
View XCTestAndNil.md

XCTest and Optional Unwrapping

XCTest is the default test harness on iOS an Apple’s other platforms. It provides support for organizing test cases and asserting expectations in your application code, and reporting the status of those expectations. It's not as fancy as some of the BDD frameworks like Quick and Cedar, but it has gotten much better than it used to be, and is my preferred test framework these days.

The Problem

One place where the XCTest assertion utilities fall a bit short has been with managing Optional variables in swift. The default use of XCTAssertNotNil don't provide any mechanism for unwrapping, easily leading to assertion checks like this:

class TestCaseDefault: XCTestCase {