Two weak variables referencing each other in Swift?

后端 未结 3 1384
滥情空心
滥情空心 2021-02-13 20:13

I\'m making another attempt today to try to understand retain cycles and weak references in Swift. Reading through the documentation, I saw the following code example where one

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 20:32

    You can do that. The only side effect is that you need to ensure that something else is retaining the people and the apartments. In the original code you just need to retain the people and the apartments (associated with people) will be retained for you.

    Strictly speaking the people aren't killed when the apartments are demolished and the apartments aren't demolished when the people die so weak references in this scenario make sense. It's generally better to consider the relationship and ownership model you want and then decide how to achieve that.

提交回复
热议问题