Skip to content

Instantly share code, notes, and snippets.

View kayoslab's full-sized avatar
:electron:
Focusing

Simon C. Krüger kayoslab

:electron:
Focusing
View GitHub Profile
@kayoslab
kayoslab / Collection+Safe.swift
Created July 8, 2020 07:55
A safe way to subscribe collection items. This should put a definite end to index-out-of-bounds errors.
import Foundation
extension Collection {
/// Safe subscription for collection elements.
///
/// - Parameter safe: The index of which the element should be returned.
/// - Returns: The element at the given index if present, otherwise nil.
subscript (safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
@kayoslab
kayoslab / UserDefaults.swift
Created July 8, 2020 08:02
A Property wrapper around UserDefaults which makes it easier to interact with.
import Foundation
let defaults = UserDefaults(suiteName: "GistApplication")
/// A Property wrapper around UserDefaults which makes it easier to interact with.
@propertyWrapper struct UserDefault<T> {
/// The key of the entry which should be added to the defaults.
let key: String