更新时间:2023-02-17 14:38:53
如果通过IBOutlet
或a作为delegate
/datasource
连接,则只能声明弱属性. >(引用另一个UIViewController
).
You would only declare a weak property if it was connected via an IBOutlet
or a as a delegate
/datasource
(references another UIViewController
).
如果您创建一个弱属性,它将在实例化后立即释放.但是,通过IBOutlet
连接的weak
属性将不会释放,因为视图牢固地持有该属性.
If you make a weak property, it will be released immediately after instantiating it. However, a weak
property connected via an IBOutlet
will not release because the view holds strongly to the property.
与VC类型的属性(例如delegate
)相同,它们是weak
属性,因为您将类分配给该属性.显然,VC是强势持有的,因此代表应避免强力持有VC,以防止保留周期(其中a
强烈持有b
而b
强烈持有a
).
Same with properties of type VCs, such as delegate
s, they are weak
properties because you assign your class to the property. The VC is obviously held strongly so the delegate should refrain from holding strongly to the VC to prevent retain cycles (where a
holds strongly to b
and b
holds strongly to a
).
因此,要回答您的问题,如果没有任何强力支持,则会立即释放weak
属性,以上是使用weak
属性的情况.
So to answer your question, a weak
property will be released immediately if nothing holds strongly to it, and the above is scenarios where you will use a weak
property.