且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在目标C中,何时在ARC下释放弱对象?

更新时间: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强烈持有bb强烈持有a).

Same with properties of type VCs, such as delegates, 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.