Created
February 27, 2017 02:58
-
-
Save AntiMoron/de0d9250e7035b307f743a38a07f3443 to your computer and use it in GitHub Desktop.
Dictionary set value for keypath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 | |
return | |
} | |
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": [ | |
"id":"123213", | |
"fuck": "tsetest" | |
]] as [String: Any] | |
setDictionary(dict: &tester, value: "444", forKey: "hahah.id") | |
print(tester) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment