Skip to content

Instantly share code, notes, and snippets.

// A view that can flip between its "front" and "back" side.
//
// Animation implementation based on:
// Chris Eidhof, Keyframe animations <https://gist.github.com/chriseidhof/ea0e435197f550b195bb749f4777bbf7>
import SwiftUI
// MARK: - Chris's keyframe animation design
struct Keyframe<Data: Animatable> {
@ole
ole / TwoColumnNavigation.swift
Created December 2, 2021 17:48
Weird SwiftUI two-column NavigationView behavior
// 1. Run on iPad simulator in landscape (= two-column navigation view).
// 2. Drill down to level 3
// 3. Observe console output.
//
// Result with .navigationViewStyle(.stack) (as expected):
// Level 1: drilledDown=false
// Level 1: drilledDown=true
// Level 2: drilledDown=false
// Level 2: drilledDown=true
// Level 3: drilledDown=false
import SwiftUI
struct ContentView: View {
@State var horizontal: Bool = true
@Namespace var namespace
var body: some View {
VStack(spacing: 40) {
if horizontal {
HStack { items }
@ole
ole / CodableContainers.swift
Created November 18, 2021 10:55
Codable: Encoding through a single-value container vs. calling `encode(to: encoder)` directly
import Foundation
struct ContainerEncoded: Encodable {
var date: Date = .now
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(date)
}
}
@ole
ole / UnicodeEquivalence.swift
Created September 27, 2021 21:42
Unicode canonical equivalence vs. compatibility
let precomposed = "é"
let decomposed = "e\u{301}"
// `==` for strings means canonically equivalent:
assert(precomposed == decomposed)
let letters = "ffi"
let ligature = "ffi"
// The ligature and its decomposition are *not* canonically equivalent:
assert(letters != ligature)
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 June 18, 2021 19:27
ISO8601 date formatting in Foundation in iOS 15/macOS 12
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 March 28, 2021 07:39
SwiftUI alignment guide layout. Why is the rectangle drawn out of bounds of the stack view?
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 }
}
/*:
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 August 7, 2020 18:51
Extract a value from the SwiftUI environment by the name of its associated EnvironmentKey (e.g. "ForegroundColorKey").

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.