Skip to content

Instantly share code, notes, and snippets.


Brian King KingOfBrian

  • Boston Massachusetts
View GitHub Profile
KingOfBrian / StringKeyDictionary.swift
Last active Mar 6, 2020
Fix encoding of key wrapper types
View StringKeyDictionary.swift
/// Codable doesn't do a good job of Dictionaries with non-String keys.
/// This property wrapper fixes things.
/// Context:
/// Tests: StringKeyDictionaryTests
public struct StringKeyDictionary<Key: RawRepresentable & Hashable & Decodable, Value: Codable>: Codable where Key.RawValue == String {
public var wrappedValue: [Key: Value]
public init(wrappedValue: [Key: Value]) {
self.wrappedValue = wrappedValue
View gist:c57b4a5b6faa57cf3876ab826c555f68
protocol AST {
associatedtype NodeType = Self
var children: [NodeType] { get }
extension AST where NodeType: AST, NodeType.NodeType == NodeType {
func traverse(check: (Self.NodeType) -> Bool) {
for child in children {
if check(child) {
child.traverse(check: check)

Describing dispatch scenarios is confusing, and spelling out the code is wordy. In an attempt to keep this document clear and compact, I'll use a short hand to describe the declaration scenarios.

Short hand:

T  = Type (class / struct / enum)
ST = Sub Type (only applies to classes)
P  = Protocol
M1.T = Type from the other module
KingOfBrian /
Last active Jun 15, 2017 — forked from erica/
Requiring Proactive Overrides for Default Protocol Implementations

Requiring Proactive Overrides for Default Protocol Implementations

  • Proposal: tbd
  • Author(s): Erica Sadun
  • Status: tbd
  • Review manager: tbd


This proposal enhances protocol implementation safety. It incorporates two keywords that cooperate with compiler checks to limit "near miss" implementation errors and accidental member overrides. This is a strictly syntactic system intended to provide greater safety at compile time, and would not affect existing compiled code bases. *** BK: Saying it does not affect implies to me that it's optional. ***

View gist:d9a1be5191059dae9708fc922172ab59
Brians-MBP:SketchyCode bking$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: SketchyCode/SketchyCode/Info.plist
new file: SketchyCode/SketchyCode/JSONBuilder/SketchClass.stencil
new file: SketchyCode/SketchyCode/JSONBuilder/SketchTypeBuilder.swift
new file: SketchyCode/SketchyCode/SketchClasses+Helpers.swift
View gist:adcecf3e24286975b687ef8997581fcd
// Ignore the first few unknowns:
struct MSAssetCollection {
let gradients: [<<Unknown>>]
let images: [<<Unknown>>]
let colors: [MSColor]
let imageCollection: MSImageCollection
struct MSImageCollection {
View 0_Failure_Backtrace
(lldb) bt
* thread #1, queue = '', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
* frame #0: 0x000000010051d687 libswiftCore.dylib`::_swift_release_dealloc(object=0x0000000101509670) at HeapObject.cpp:558
frame #1: 0x0000000100528166 libswiftCore.dylib`bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::doDecrementSlow<(this=0x0000000101509678, oldbits=(bits = 0), dec=1)0, (swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) at RefCount.h:1046
frame #2: 0x0000000100526b8c libswiftCore.dylib`bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::doDecrement<(this=0x0000000101509678, dec=1)0, (swift::PerformDeinit)1>(unsigned int) at RefCount.h:1104
frame #3: 0x0000000100517381 libswiftCore.dylib`::_swift_release_(swift::HeapObject *) [inlined] swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1> >::decrementAndMaybeDeinit(this=0x0000000101509678, dec=1) at RefCount.h:907
KingOfBrian / 1_Error
Last active Mar 11, 2017
Local Test Failures
View 1_Error
Some errors are masked a bit. This one is pretty clear:
FAIL: Swift(macosx-x86_64) :: attr/open.swift (2514 of 2903)
******************** TEST 'Swift(macosx-x86_64) :: attr/open.swift' FAILED ********************
rm -rf /Users/bking/sandbox/swift-source/build/Ninja-DebugAssert/swift-macosx-x86_64/test-macosx-x86_64/attr/Output/open.swift.tmp && mkdir -p /Users/bking/sandbox/swift-source/build/Ninja-DebugAssert/swift-macosx-x86_64/test-macosx-x86_64/attr/Output/open.swift.tmp
xcrun --toolchain default --sdk /Applications/ swiftc -target x86_64-apple-macosx10.9 -module-cache-path '/var/folders/33/wl32_mc90t58zkt8sn1sm_sc0000gn/T/swift-testsuite-clang-module-cachecpuIWO' -F /Applications/ -Xlinker -rpath -Xlinker /Applications/
KingOfBrian /
Last active Feb 14, 2017
XCTest and nil assertions

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 {
KingOfBrian /
Last active Mar 12, 2017
Remove support for final in protocol extensions

Remove final support in protocol extensions


This proposal disallows the final keyword when declaring functions in protocol