Skip to content

Instantly share code, notes, and snippets.

View LazyFilterSequenceIndices.swift
let evens = Array(1...10)
.lazy
.filter { $0.isMultiple(of: 2) }
evens.indices // → [1, 3, 5, 7, 9]
evens[0] // → 1 (🤯!)
@ole
ole / DateFormatting.swift
Created Jun 18, 2021
ISO8601 date formatting in Foundation in iOS 15/macOS 12
View DateFormatting.swift
import Foundation
let date = Date.now
date.formatted(.iso8601) // "20210618T191800Z"
date.formatted(.iso8601.year().month().day().dateSeparator(.dash)) // "2021-06-18"
date.formatted(.iso8601.dateSeparator(.dash).timeSeparator(.colon)) // "2021-06-18T19:18:00Z"
@ole
ole / AlignmentGuideLayout.swift
Created Mar 28, 2021
SwiftUI alignment guide layout. Why is the rectangle drawn out of bounds of the stack view?
View AlignmentGuideLayout.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
// Why is the rectangle drawn out of bounds of the stack view?
Rectangle().fill(Color.green)
.frame(width: 100, height: 40)
.alignmentGuide(HorizontalAlignment.center) { _ in 0 }
}
View TypedHeterogeneousDictionaries.swift
/*:
Joe Groff:
> In a lot of the ways people use dictionaries/maps/hashtables, there's a type dependency between
> the keys and values, but most mainstream typed languages provide homogeneously-typed maps.
> Are there any that try to preserve more interesting type relationships between key and value?
> https://twitter.com/jckarter/status/1278739951568314368
> As one example, it's common for specific keys to be associated with specific types, like in
> {"name": "Tanzy", "age": 13}, "name" should always map to a string, and "age" to a number.
@ole
ole / README.md
Created Aug 7, 2020
Extract a value from the SwiftUI environment by the name of its associated EnvironmentKey (e.g. "ForegroundColorKey").
View README.md

Extract a value from the SwiftUI environment by the name of its associated EnvironmentKey (e.g. "ForegroundColorKey").

Tested only in Xcode 12.0 beta 4 in the iOS simulator. May break in other environments because it uses reflection. If SwiftUI's private type hierarchy changes, it will probably stop working.

@ole
ole / !swiftui-reflection-dump.md
Last active Jul 6, 2021
A dump of the SwiftUI.framework binary for the iOS simulator (as of Xcode 12.0 beta 2) using the swift-reflection-dump tool.
View !swiftui-reflection-dump.md

A dump of the SwiftUI.framework binary for the iOS simulator (as of Xcode 12.0 beta 2) using the swift-reflection-dump tool.

Note: I used a Swift 5.3 compiler build from a few weeks ago that I had laying around. Because of ABI stability, I don't think the swift-reflection-dump version has to match the compiler version that was used to build the binary, but I'm not 100% sure.

View remove-unused-namespaces.xslt
<!--
XSLT for removing unused namespaces from an XML file.
Author: Dimitre Novatchev
Source: https://stackoverflow.com/a/4594626
License: CC BY-SA, https://creativecommons.org/licenses/by-sa/2.5/
Usage:
xmlstarlet tr remove-unused-namespaces.xslt -
@ole
ole / NotificationObserver.swift
Last active Feb 17, 2021
Demo that retaining self in a NotificationCenter observer block leads to a reference cycle you have to break manually.
View NotificationObserver.swift
// Paste into a macOS playground in Xcode and run.
// Deleting line 46 "c?.stopObserving()" triggers the assertion because
// the reference cycle caused by retaining self in the observer block is never broken.
import Foundation
let myNotification = NSNotification.Name(rawValue: "myNotification")
var cHasBeenDeallocated = false
@ole
ole / KeyedContainer.swift
Created Dec 10, 2019
A property wrapper for dictionaries with keys that are raw-representable as strings. It modifies the wrapped dictionary's encoding/decoding behavior such that the dictionary is encoded as a dictionary (unkeyed container) rather than as an array (keyed container). For context, see https://oleb.net/blog/2017/12/dictionary-codable-array/
View KeyedContainer.swift
import Foundation
// MARK: - KeyedContainer
/// A property wrapper for dictionaries with keys that are raw-representable as strings.
/// It modifies the wrapped dictionary's encoding/decoding behavior such that the dictionary
/// is encoded as a dictionary (unkeyed container) rather than as an array (keyed container).
///
/// For context, see <https://oleb.net/blog/2017/12/dictionary-codable-array/>.
@propertyWrapper
@ole
ole / combine-urlsession.swift
Last active Nov 7, 2019
Combine with URLSession.dataTaskPublisher
View combine-urlsession.swift
// https://twitter.com/BelleBCooper/status/1192173933983715328
import Combine
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
/// JSON format:
///