Skip to content

Instantly share code, notes, and snippets.

Created Feb 27, 2017
What would you like to do?
swift3 set value for keypath
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
fileprivate func _setDictionary( dict:inout [String: Any], value: Any, forKeys: ArraySlice<String>) {
guard forKeys.count > 1 else {
dict[forKeys.first!] = value
if var subdict = dict[forKeys.first!] as? [String: Any] {
_setDictionary(dict: &subdict, value: value, forKeys: forKeys[1..<forKeys.count])
dict[forKeys.first!] = subdict
func setDictionary( dict:inout [String: Any], value: Any, forKey: String) {
let keypaths = forKey.components(separatedBy: ".")
_setDictionary(dict: &dict, value: value, forKeys: keypaths[0..<keypaths.count])
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