Skip to content

Instantly share code, notes, and snippets.

@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.
@ole
ole / AsyncOperation.swift
Created August 19, 2018 16:47
An (NS)Operation subclass for async operations
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must override `main()` to perform any work and call `finish()`
/// when they are done. All `NSOperation` work will be handled automatically.
///
/// Source/Inspiration: https://stackoverflow.com/a/48104095/116862 and https://gist.github.com/calebd/93fa347397cec5f88233
open class AsyncOperation: Operation {
public init(name: String? = nil) {
super.init()
@ole
ole / UIAlertController+TextField.swift
Last active January 24, 2025 19:07
A UIAlertController with a text field and the ability to perform validation on the text the user has entered while the alert is on screen. The OK button is only enabled when the entered text passes validation. More info: https://oleb.net/2018/uialertcontroller-textfield/
import UIKit
/// A validation rule for text input.
public enum TextValidationRule {
/// Any input is valid, including an empty string.
case noRestriction
/// The input must not be empty.
case nonEmpty
/// The enitre input must match a regular expression. A matching substring is not enough.
case regularExpression(NSRegularExpression)
/// Ignores `value` and just evaluates `pattern`.
/// Can be used to evaluate arbitrary expressions in a switch.
func ~=<A>(pattern: @autoclosure () -> Bool, value: A) -> Bool {
return pattern()
}
let (x, y, z) = (0, 0, 1)
switch () /* can be anything */ {
case x > 0: print("x matches")
import UIKit
let containingRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let path1 = UIBezierPath(rect: containingRect)
let circleRect = CGRect(x: 50, y: 10, width: 80, height: 80)
let circleCenter = CGPoint(x: circleRect.midX, y: circleRect.midY)
let radius = circleRect.height / 2
/*
@ole
ole / Mojave-dynamic-wallpaper-notes.md
Last active November 3, 2024 20:05
Reverse-engineering the dynamic wallpaper file format in macOS Mojave.

The dynamic wallpaper in MacOS Mojave is a single 114 MB .heic file that seems to contain 16 embedded images.

It also contains the following binary plist data in its metadata under the key "Solar". It's an array of 16 items, each with four keys:

  • i (integer). This seems to be the image index.
  • o (integer). This is always 1 or 0. Stephen Radford thinks it indicates dark mode (0) vs. light mode (1).
  • a (decimal). I’m pretty sure this is the angle of the sun over the horizon. 0º = sunset/sunrise. 90º = sun directly overhead. Negative values = sun below horizon.
  • z (decimal). This seems to be the cardinal position of the sun relative to the camera. 0º = sun is directly in front of the camera. 90º = sun is directly to the right of the camera. 180º = sun is directly behind the camera.
@ole
ole / adjustInfoPlist.sh
Last active April 23, 2018 14:53
A script for setting `UIFileSharingEnabled` and `LSSupportsOpeningDocumentsInPlace` in Xcode debug builds.
#!/bin/sh
if ( [ "$CONFIGURATION" == "Debug" ] ) then
echo "Enabling filesharing flags for '$CONFIGURATION' configuration in "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}""
/usr/libexec/PlistBuddy -c "Set :UIFileSharingEnabled true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :LSSupportsOpeningDocumentsInPlace true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
fi