Skip to content

Instantly share code, notes, and snippets.

View beccadax's full-sized avatar

Becca Royal-Gordon beccadax

View GitHub Profile
@beccadax
beccadax / JSONFeed.swift
Last active June 18, 2020 08:21
JSON Feed parser in Swift 4—now updated for Xcode 9 Beta 1. Uses the new Codable protocol and JSONDecoder class.
import Foundation
let feedURL = URL(string: "https://daringfireball.net/feeds/json")!
// MARK: Basic models
// We do not model the `userComment` field because it is not supposed to be used by
// machines. We do not model extensions because they should be ignored unless
// supported anyway.
struct Feed {
print("""
<?xml version="1.0"?>
<catalog>
\(bookTuples.map(xmlForBookTuple).joined(separator: "")
)</catalog>
""")
typealias ChapterTuple = (heading: String)
typealias BookTuple = (id: String, author: String, title: String, genre: String, price: String, chapters: [ChapterTuple])
@beccadax
beccadax / Formatter.swift
Last active March 26, 2017 09:15
An example formatter design.
protocol Formatter {
associatedtype Input
associatedtype Output
func format(_ input: Input) -> Output
}
protocol CombiningFormatter: Formatter where
InnerFormatter.Output == CombinedFormatter.Input
{
@beccadax
beccadax / LocalizableString-new-interpolation.swift
Created March 11, 2017 01:44
An implementation of a LocalizableString type in the Swift new-interpolation branch: https://github.com/brentdax/swift/tree/new-interpolation
import Foundation
public protocol LocalizableArgument {
var localizableFormat: String { get }
var localizableFormatArguments: [CVarArg] { get }
}
extension LocalizableArgument where Self: CVarArg {
public var localizableFormatArguments: [CVarArg] {
return [self]

Fix ExpressibleByStringInterpolation

Introduction

String interpolation is a simple and powerful feature for expressing

The problem with Metatype as a keyword is that it's one level of abstraction too far up.

Think about a typical type name: Int, UIViewController, Record. What you'll notice about the type name is that it's a description of the instances of that type, not a description of the type itself. You don't see types with names like IntStruct or UIViewControllerClass except in beginner code.

Just as UIViewController is a class, UIViewController.Type (to use current terminology) is a metatype. But that doesn't mean the word "metatype" belongs in its name, any more than the word "class" belongs in UIViewController. The type name describes the instances of that type, and the instances of UIViewController.Type are types, so Type, not Metatype, is the appropriate word.

That's why I say that we should use Type<T>, not Metatype<T>—because Metatype<T> would have the same mistake as UIViewControllerClass.

@beccadax
beccadax / Example.swift
Created January 21, 2017 09:43
`UnsafeNulTerminatedBufferPointer` is a Swift type which turns an `UnsafePointer` to zero-terminated data into a `Collection`.
"hello world".withCString { cString in
// Horribly, `withCString` gives you a pointer to `Int8`, everything else wants
// `UInt8`, and the safe way to do this, using `withMemoryRebound`, wants a length.
// `unsafeBitCast` is technically invalid but happens to be work in this demo.
let workaroundCString = unsafeBitCast(cString, to: UnsafePointer<UInt8>.self)
let buffer = UnsafeNulTerminatedBufferPointer(start: workaroundCString)
buffer.count
print(String(buffer, encoding: UTF8.self))
}
extension Dictionary {
subscript(digging first: Key, rest: AnyHashable...) -> Any? {
return self[digging: first, rest[0..<rest.endIndex]]
}
subscript(digging first: Key, rest: ArraySlice<AnyHashable>) -> Any? {
let v = self[first]
guard let firstOfRest = rest.first else {
return v
final class AssociatedValue<Value, Object: NSObject> {
init(_ memoryManagement: AssociatedValueMemoryManagement, ofType valueType: Value.Type, on objectType: Object.Type) {
self.memoryManagement = memoryManagement
}
let memoryManagement: AssociatedValueMemoryManagement
var key: UnsafeRawPointer {
return UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())
}
@beccadax
beccadax / gist:8918c9f6ff8f19ad1e24f9896e1b3514
Last active October 8, 2016 04:12
"Trek Against Trump" cast list
Last updated: 7 October 2016
All: 37/47
S = Signed
- = Didn't sign
f = Foreign citizen
d = Deceased
TOS: (2/3)
f Kirk: William Shatner