Skip to content

Instantly share code, notes, and snippets.

View KingOfBrian's full-sized avatar

Brian King KingOfBrian

  • Boston Massachusetts
View GitHub Profile
@KingOfBrian
KingOfBrian / StringKeyDictionary.swift
Last active March 6, 2020 15:27
Fix encoding of key wrapper types
/// Codable doesn't do a good job of Dictionaries with non-String keys.
/// This property wrapper fixes things.
///
/// Context: https://oleb.net/blog/2017/12/dictionary-codable-array/
/// Tests: StringKeyDictionaryTests
@propertyWrapper
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
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
KingOfBrian / overrides.md
Last active June 15, 2017 19:52 — forked from erica/overrides.md
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

Introduction

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. ***

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
// Ignore the first few unknowns:
struct MSAssetCollection {
let gradients: [<<Unknown>>]
let images: [<<Unknown>>]
let colors: [MSColor]
let imageCollection: MSImageCollection
}
struct MSImageCollection {
@KingOfBrian
KingOfBrian / 0_Failure_Backtrace
Last active April 6, 2019 17:11
Test Failures
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', 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
KingOfBrian / 1_Error
Last active March 11, 2017 19:25
Local Test Failures
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 ********************
Script:
--
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/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk swiftc -target x86_64-apple-macosx10.9 -module-cache-path '/var/folders/33/wl32_mc90t58zkt8sn1sm_sc0000gn/T/swift-testsuite-clang-module-cachecpuIWO' -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/../../../Developer/Library/Frameworks -Xlinker -rpath -Xlinker /Applications/Xcode-beta.app/Contents/Developer/Plat
@KingOfBrian
KingOfBrian / XCTestAndNil.md
Last active January 8, 2024 19:34
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
KingOfBrian / Remove_final_support_in_protocol_extensions.md
Last active March 12, 2017 02:08
Remove support for final in protocol extensions

Remove final support in protocol extensions

Introduction

This proposal disallows the final keyword when declaring functions in protocol