Skip to content

Instantly share code, notes, and snippets.

View JohnSundell's full-sized avatar

John Sundell JohnSundell

View GitHub Profile
@JohnSundell
JohnSundell / OnboardingManager.swift
Last active May 21, 2020 08:19
An example of using #function for user defaults properties, and a test that guards against property name changes
import UIKit
class OnboardingManager {
private let userDefaults: UserDefaults
init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
func presentOnboardingControllerIfNeeded(in viewController: UIViewController) {
@JohnSundell
JohnSundell / Perform.swift
Last active December 21, 2019 14:43
A function that enables you to easily wrap throwing APIs, to provide a custom error
/**
* Perform a throwing expression, and throw a custom error in case the expression threw
*
* - parameter expression: The expression to execute
* - parameter error: The custom error to throw instead of the expression's error
* - throws: The given error
* - returns: The return value of the given expression
*/
func perform<T>(_ expression: @autoclosure () throws -> T, orThrow errorExpression: @autoclosure () -> Error) throws -> T {
do {
@JohnSundell
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
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 / GenerateLicenses.swift
Last active April 19, 2019 18:28
A Swift script that we use at Hyper to generate licenses for used 3rd party libraries, using https://github.com/mono0926/LicensePlist
import Foundation
import Files // marathon:https://github.com/JohnSundell/Files.git
import ShellOut // marathon:https://github.com/JohnSundell/ShellOut.git
// MARK: - Generation
guard let settingsBundle = try? Folder.current.subfolder(named: "Settings.bundle") else {
print("Create a Settings.bundle in Xcode, then re-run this script")
exit(1)
}
func testLoadingData() {
class NetworkEngineMock: NetworkEngine {
typealias Handler = NetworkEngine.Handler
var requestedURL: URL?
func performRequest(for url: URL, completionHandler: @escaping Handler) {
requestedURL = url
let data = “Hello world”.data(using: .utf8)
@JohnSundell
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
command=$1
shift
case $command in
"init" )
swift package init "$@"
@JohnSundell
JohnSundell / gh
Created April 30, 2018 07:51
Script that makes it easy to show a Git repo on GitHub.com
#!/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 / EnumSequence.swift
Created August 10, 2017 15:47
A simple way to iterate over linear Int-based enums
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 / WrappingSingletons.swift
Last active October 26, 2017 18:23
This is a great technique if you need to interact with singleton-based APIs but still have great testability
import UIKit
// Create a protocol that defines what APIs that you need from the singleton
protocol Application {
func open(url: URL)
}
// Make the singleton-based class conform to your protocol
extension UIApplication: Application {
func open(url: URL) {
@JohnSundell
JohnSundell / xcode-switch.sh
Created June 17, 2017 13:29
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'"
exit
fi