Skip to content

Instantly share code, notes, and snippets.

View JohnSundell's full-sized avatar

John Sundell JohnSundell

View GitHub Profile
JohnSundell / SwiftUITimers.swift
Last active March 23, 2023 12:02
Timer-based example used during my Advanced SwiftUI workshop.
import SwiftUI
// Model + controller:
struct NamedTimer: Identifiable, Equatable {
let id = UUID()
var name: String
var seconds = 0
JohnSundell / ListViewModel.swift
Last active February 17, 2023 15:27
Example view model class used during my Advanced SwiftUI workshop
import SwiftUI
struct Item: Equatable, Identifiable {
let id = UUID()
var title: String
var isFavorite = false
@MainActor class ListViewModel: ObservableObject {
@Published private(set) var items: [Item] = []
JohnSundell / ContentViewWithCollapsableHeader.swift
Last active April 25, 2024 06:41
A content view which renders a collapsable header that adapts to the current scroll position. Based on OffsetObservingScrollView from
import SwiftUI
/// View that observes its position within a given coordinate space,
/// and assigns that position to the specified Binding.
struct PositionObservingView<Content: View>: View {
var coordinateSpace: CoordinateSpace
@Binding var position: CGPoint
@ViewBuilder var content: () -> Content
var body: some View {
JohnSundell / StarPlane.swift
Created July 8, 2020 22:41
A simple game written in SwiftUI. Note that this is just a fun little hack, the code is not meant to be taken seriously, and only works on iPhones in portrait mode.
// A fun little game written in SwiftUI
// Copyright (c) John Sundell 2020, MIT license.
// This is a hacky implementation written just for fun.
// It's only verified to work on iPhones in portrait mode.
import SwiftUI
final class GameController: ObservableObject {
@Published var plane = GameObject.plane()
@Published private(set) var clouds = [GameObject]()
JohnSundell / spm
Created August 25, 2018 18:14
A script that makes it easier to use the Swift Package Manager by making common commands less verbose 👍
#!/usr/bin/env bash
# Put this file in /usr/local/bin and then run chmod +x on it to make it executable
case $command in
"init" )
swift package init "$@"
JohnSundell / gh
Created April 30, 2018 07:51
Script that makes it easy to show a Git repo on
#!/usr/bin/env bash
# If run without a name, open any current repo
if [ -z "$1" ]; then
URL=$(git remote get-url origin)
open $URL
open "$1"
JohnSundell / simrecord
Created March 16, 2018 21:05
🎥 Script that lets you start a video recording from the iOS simulator with one command
FILENAME="$HOME/Desktop/Simulator Recording.$EXTENSION"
while [ -e "$FILENAME" ]
FILENAME="$HOME/Desktop/Simulator Recording $ITERATION.$EXTENSION"
JohnSundell / Optional+Require.swift
Created November 27, 2017 18:05
Extension that lets you require an optional to be non-nil, or throw an error (which is customizable)
extension Optional {
struct RequireError: Error, CustomStringConvertible {
var description: String {
return "Required optional value was nil"
func require(orThrow errorClosure: @autoclosure () -> Error = RequireError()) throws -> Wrapped {
switch self {
case .some(let value):
JohnSundell / CrossPlatformImages.swift
Last active November 22, 2022 06:29
An easy way to make code that uses UIImage cross-platform between iOS/tvOS & macOS
// Either put this in a separate file that you only include in your macOS target
// or wrap the code in #if os(macOS) / #endif
import Cocoa
// Step 1: Typealias UIImage to NSImage
typealias UIImage = NSImage
// Step 2: You might want to add these APIs that UIImage has but NSImage doesn't.
extension NSImage {
JohnSundell / Podfile
Last active September 4, 2019 15:20
A Podfile that demonstrates how to use dependencies that use an older Swift version
target 'MyTarget' do
# Post installation script that enables the Swift 4.2 compiler's
# legacy 4.1 mode for 4.2-incompatible pods
post_install do |installer|
incompatiblePods = ['PodA', 'PodB']
installer.pods_project.targets.each do |target|
if incompatiblePods.include?