Skip to content

Instantly share code, notes, and snippets.

@ole
ole / combine-urlsession.swift
Last active November 7, 2019 02:53
Combine with URLSession.dataTaskPublisher
// https://twitter.com/BelleBCooper/status/1192173933983715328
import Combine
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
/// JSON format:
///
@ole
ole / x-by-y-operator.swift
Created October 29, 2019 09:30
Using × as a custom operator for creating "x-by-y" values such as CGPoint or CGSize.
precedencegroup XByYPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ×: XByYPrecedence // U+00D7 MULTIPLICATION SIGN
// ------
import CoreGraphics
@ole
ole / let.swift
Created September 9, 2019 15:14
A replacement for let bindings in Swift function builders
import SwiftUI
func `let`<Value, Return>(_ expression: Value, body: (Value) -> Return) -> Return {
body(expression)
}
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
`let`(geometry.size.width / 2) { lineWidth in
@ole
ole / Atomic.swift
Created June 6, 2019 13:27
Atomic as a property wrapper
import Dispatch
import PlaygroundSupport
@propertyDelegate
struct Atomic<A> {
private var _value: A
private let queue = DispatchQueue(label: "property wrapper")
init(initialValue: A) {
_value = initialValue
@ole
ole / mount.txt
Created June 6, 2019 12:58
macOS Catalina mounts the read-only system volume at / and the read-write data volume at /System/Volumes/Data
% mount
/dev/disk1s6 on / (apfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s5 on /System/Volumes/Data (apfs, local, journaled, nobrowse)
/dev/disk1s4 on /private/var/vm (apfs, local, journaled, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
% ls /System/Volumes/Data
Device Users
@ole
ole / load-misaligned.swift
Last active June 25, 2019 21:54
Load misaligned data from a blob of bytes. I don't know if this is correct.
import Foundation
let data = Data([0, 0, 0, 0, 0, 0x11, 0x22, 0x33, 0x44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
data.count
extension UnsafeRawBufferPointer {
func loadUnaligned<T>(fromByteOffset offset: Int, as: T.Type) -> T {
// Allocate correctly aligned memory and copy bytes there
let alignedPointer = UnsafeMutableRawPointer.allocate(byteCount: MemoryLayout<T>.stride, alignment: MemoryLayout<T>.alignment)
defer {
@ole
ole / date-format-strings.swift
Last active October 8, 2020 04:25
Type-safe date format strings using string interpolation. Requires Swift 5.0.
// Type-safe date format strings using string interpolation
// Requires Swift 5.0.
import Foundation
enum DateFormatComponent {
case era(AlphaStyle)
case year(MinimumDigits)
case yearForWeekOfYear(MinimumDigits)
case quarter(AlphaNumericStyle)
@ole
ole / amazon-transcribe-swift-community-podcast-0001.md
Created January 23, 2019 21:42
Swift Community Podcast Episode 1: Transcript (autogenerated with the Amazon Transcribe service)

0.54–21.25
John Sundell Welcome to the very first episode of the Swift Community Podcast. A show for the Swift community by the Swift Community. I am one of your hosts, John Sindel. And with me, I have two wonderful co hosts, the first of which you might know as the host of the Swift coders podcast. Mr. Garric Nahapetian. How's going Garric?

21.38–24.27
Garric Nahapetian Excellent. Thank you so much for having me. How you doing?

24.46–35.97
John Sundell I'm doing great. And we also have our third and final co host for this episode. And it's none other than the creator of Swift itself. Mr Chris Lattner. How's it going, Chris?

35.98–40.75

@ole
ole / objc-extension-override-without-dynamic.swift
Created January 11, 2019 12:57
Overriding at-objc methods defined in extensions without the dynamic keyword
// Paste this into an iOS playground in Xcode and run
// Expected output:
// ParentVC: handled in ParentCoordinator
// ChildVC: handled in ChildCoordinator
// UIViewController: accountLogin() not handled
import UIKit
NSSetUncaughtExceptionHandler { exception in
print("Exception: \(exception.name) — \(exception)")
@ole
ole / headAndTail.swift
Last active November 7, 2018 14:18
Swift challenge: Sequence.headAndTail. Context: https://twitter.com/olebegemann/status/1059923129345196032
// Challenge: Fill out this extension
//
// I have a solution for this return type:
//
// var headAndTail: (head: Element, tail: AnySequence<Element>)?
//
// I don't think it can be done with tail: SubSequence.
extension Sequence {
/// Destructures `self` into the first element (head) and the rest (tail).
/// Returns `nil` if the sequence is empty.