Changing value in nested dictionary in swift

前端 未结 3 1656
抹茶落季
抹茶落季 2021-01-05 09:00

I am wondering why when setting the value of a nested dictionary, the containing dictionary does not reflect those changes? On line3, is a copy of the dictionary being retur

3条回答
  •  既然无缘
    2021-01-05 09:39

    Yes, in swift structs are passed by value not reference. In other words, you are getting a copy, with the same values. As I understand it, it actually does some optimization under the hood, whereby it doesn't actually make the copy until you change something. Regardless, what you get is a separate instance of the dictionary.

    Same thing goes for arrays.

提交回复
热议问题