Skip to content

Instantly share code, notes, and snippets.

John Sundell JohnSundell

Block or report user

Report or block JohnSundell

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
JohnSundell / spm
Created Aug 25, 2018
A script that makes it easier to use the Swift Package Manager by making common commands less verbose 👍
View spm
#!/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 Apr 30, 2018
Script that makes it easy to show a Git repo on
View gh
#!/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 Mar 16, 2018
🎥 Script that lets you start a video recording from the iOS simulator with one command
View simrecord
FILENAME="$HOME/Desktop/Simulator Recording.$EXTENSION"
while [ -e "$FILENAME" ]
FILENAME="$HOME/Desktop/Simulator Recording $ITERATION.$EXTENSION"
JohnSundell / Optional+Require.swift
Created Nov 27, 2017
Extension that lets you require an optional to be non-nil, or throw an error (which is customizable)
View Optional+Require.swift
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 Mar 30, 2020
An easy way to make code that uses UIImage cross-platform between iOS/tvOS & macOS
View CrossPlatformImages.swift
// 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 Sep 4, 2019
A Podfile that demonstrates how to use dependencies that use an older Swift version
View Podfile
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?
JohnSundell / AnyOf.swift
Created Aug 21, 2017
A way to easily compare a given value against an array of candidates
View AnyOf.swift
import Foundation
struct EquatableValueSequence<T: Equatable> {
static func ==(lhs: EquatableValueSequence<T>, rhs: T) -> Bool {
return lhs.values.contains(rhs)
static func ==(lhs: T, rhs: EquatableValueSequence<T>) -> Bool {
return rhs == lhs
JohnSundell / EnumSequence.swift
Created Aug 10, 2017
A simple way to iterate over linear Int-based enums
View EnumSequence.swift
struct EnumSequence<T: RawRepresentable> where T.RawValue == Int {}
extension EnumSequence: Sequence {
func makeIterator() -> AnyIterator<T> {
var rawValue = 0
return AnyIterator {
let nextCase = T(rawValue: rawValue)
rawValue += 1
return nextCase
JohnSundell /
Created Jun 17, 2017
A script that switches between the App Store & beta versions of Xcode
#!/usr/bin/env bash
# This script switches between the App Store version of Xcode and the beta
# Install it by copying it to /usr/local/bin/xcode-switch and running 'chmod +x' on it (to make it executable)
# Then run it using 'sudo xcode-switch'
if [ "$EUID" -ne 0 ]; then
echo "xcode-select requires you to run this script as root; 'sudo xcode-switch'"
JohnSundell / DictionaryMap.swift
Created Jun 5, 2017
A map method for Dictionary that lets you easily transform its keys and values into other types
View DictionaryMap.swift
extension Dictionary {
func map<K: Hashable, V>(_ transform: (Element) throws -> (key: K, value: V)) rethrows -> [K : V] {
var transformed = [K : V]()
for pair in self {
let transformedPair = try transform(pair)
transformed[transformedPair.key] = transformedPair.value
return transformed
You can’t perform that action at this time.