Skip to content

Instantly share code, notes, and snippets.

Created Feb 27, 2017
What would you like to do?
Dictionary set value for keypath
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
func setDictionary( dict:inout [String: Any], value: Any, forKey: String) {
let keypaths = forKey.components(separatedBy: ".")
guard keypaths.count > 1 else {
dict[keypaths[0]] = value
if var subdict = dict[keypaths.first!] as? [String: Any] {
setDictionary(dict: &subdict, value: value, forKey: keypaths[keypaths.startIndex.advanced(by: 1)..<keypaths.endIndex].joined(separator: "."))
dict[keypaths.first!] = subdict
var tester = ["hahah": [
"fuck": "tsetest"
]] as [String: Any]
setDictionary(dict: &tester, value: "444", forKey: "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment