Skip to content

Instantly share code, notes, and snippets.

@casperzandbergenyaacomm
casperzandbergenyaacomm / DictionaryKeyPath.swift
Last active June 14, 2021 11:16 — forked from dfrib/DictionaryKeyPath.swift
Swift: Reading and writing to (possible) nested dictionaries for a given key path, using a recursive approach
// Inspired by: https://gist.github.com/dfrib/d7419038f7e680d3f268750d63f0dfae
import Foundation
extension Dictionary {
subscript(keyPath string: String) -> Value? {
get {
return self[keyPath: Dictionary.keyPath(for: string)]
}
set {
self[keyPath: Dictionary.keyPath(for: string)] = newValue
@casperzandbergenyaacomm
casperzandbergenyaacomm / EmptyOrNil.swift
Last active November 12, 2021 08:10
Adding readability for an if statement I often use.
protocol EmptyOrNil {
var isEmpty: Bool { get }
}
extension Optional where Wrapped: EmptyOrNil {
var isEmptyOrNil: Bool {
return self?.isEmpty ?? true
}
}
@louisdh
louisdh / NYSKeyboardHelper.swift
Last active March 10, 2023 21:47 — forked from matthiasnys/NYSKeyboardHelper.swift
A Keyboard helper for your Swift Keyboard troubles.
//
// NYSKeyboardHelper.swift
// B-NYS GCV
//
// Created by Matthias Nys on 18/03/2017.
// Copyright © 2017 B-NYS. All rights reserved.
//
import UIKit
@dfrib
dfrib / DictionaryKeyPath.swift
Last active April 14, 2023 12:45
Swift: Reading and writing to (possible) nested dictionaries for a given key path, using a recursive approach
// For details, see
// http://stackoverflow.com/questions/40261857/remove-nested-key-from-dictionary
import Foundation
extension Dictionary {
subscript(keyPath keyPath: String) -> Any? {
get {
guard let keyPath = Dictionary.keyPathKeys(forKeyPath: keyPath)
else { return nil }
return getValue(forKeyPath: keyPath)