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
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
command=$1
shift
case $command in
"init" )
swift package init "$@"
@JohnSundell
JohnSundell / gh
Created Apr 30, 2018
Script that makes it easy to show a Git repo on GitHub.com
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)
URL=${URL/"git@github.com:"/"https://github.com/"}
URL=${URL/".git"/""}
open $URL
else
open "https://github.com/$1"
@JohnSundell
JohnSundell / simrecord
Created Mar 16, 2018
🎥 Script that lets you start a video recording from the iOS simulator with one command
View simrecord
#!/bin/bash
ITERATION=1
EXTENSION="mp4"
FILENAME="$HOME/Desktop/Simulator Recording.$EXTENSION"
while [ -e "$FILENAME" ]
do
ITERATION=$((ITERATION+1))
FILENAME="$HOME/Desktop/Simulator Recording $ITERATION.$EXTENSION"
@JohnSundell
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
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
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
use_frameworks!
# 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? target.name
@JohnSundell
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
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
JohnSundell / xcode-switch.sh
Created Jun 17, 2017
A script that switches between the App Store & beta versions of Xcode
View xcode-switch.sh
#!/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'"
exit
fi
@JohnSundell
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.