1

I would like to set the value of a key of a key in swift 4. Is this possible?

For example if we have

var d:[String:Any] = [:]

I see there is a function 'updateValue'

d.updateValue([:], forKey: "a")

But what I want to be able to do is:

d.updateValue([:], forKey: "a.b.c")

which would be expected to resolve to

{"a" : { "b" : { "c" : {} }

instead of

{"a.b.c" : {} } 

thanks

Willeke
  • 14,578
  • 4
  • 19
  • 47
aman
  • 499
  • 8
  • 18

1 Answers1

1

Dictionary does not support this out the box but you could use keypaths. https://oleb.net/blog/2017/01/dictionary-key-paths/

Your best bet is to create nested dictionaries or a custom JSON type. https://stackoverflow.com/a/25475702/3705470

Chéyo
  • 9,107
  • 9
  • 27
  • 44