简体   繁体   English

Swift 中 unowned 和 weak 但隐式解包的区别是什么?

[英]What's the difference in Swift between unowned vs weak but implicitly unwrapped?

weak var cat: Cat!

vs对比

unowned var cat: Cat

Just started getting my head around this topic but this one really confuses me, is there any benefits of using weak but implicitly unwrapped variable in normal setting, other than for UI stuff?刚刚开始了解这个话题,但这个话题真的让我感到困惑,除了 UI 内容之外,在正常设置中使用弱但隐式解包的变量有什么好处吗?

  1. You can still check an IUO to see if it's nil, even though you don't have to.您仍然可以检查 IUO 以查看它是否为零,即使您不必这样做。
  2. You have to give an unowned var an initial value that isn't just none .你必须给一个unowned var一个不只是none的初始值。 This is not as meaningful as none if the initial value is just a placeholder before it gets a "real" value.如果初始值在获得“真实”值之前只是一个占位符,则这不像none那样有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM