菜鸟教程小白 发表于 2022-12-12 16:14:47

ios - Objective C 中的 __strong 用法示例


                                            <p><p>我在这里阅读了关于 __strong 引用和 __weak 引用的用法:<a href="https://stackoverflow.com/questions/9262535/explanation-of-strong-and-weak-storage-in-ios5" rel="noreferrer noopener nofollow">Explanation of strong and weak storage in iOS5</a> </p>

<p>我尝试编写一些代码来展示这些知识。但是,__strong 在对象被释放时并未将其保留在内存中。
我第一次这样做:</p>

<pre><code>Parent *fumu = [ init];
;
</code></pre>

<p>一切都按预期进行。调用父对象init,释放时调用dealloc。</p>

<p>第二次我这样做了:</p>

<pre><code>Parent *fumu = [ init];
;
;
</code></pre>

<p>调用了父对象的 init 方法。但是由于 fumu 引用的 Parent 对象的保留计数仍然为 1,所以没有调用 dealloc。正如预期的那样。</p>

使用 __strong

<p>如前所述:</p>

<pre>**Strong: &#34;keep this in the heap until I don&#39;t point to it anymore&#34;
Weak: &#34;keep this as long as someone else points to it strongly&#34;**
</pre>

<p>Now let&#39;s say I use __strong keyword. If I add another strong reference like below, the Parent object should NOT call dealloc because we still have a strong reference (anotherFumu) to it. However, when I run it, the dealloc gets called. I do not see the strong reference having any effect.</p>

<pre><code>Parent *__strong fumu = [ init];
Parent *__strong anotherFumu = fumu;
;//Parent object dealloc gets called
</code></pre>

<p>请指教。谢谢</p>

<p><strong>结果:</strong></p>

<p>我打开了 ARC,并简单地使用 nil 将强指针指向远离 Parent 对象,从而能够正确地看到 __strong 和 __weak 的行为,如下所示:</p>

<pre><code>Parent * fumu = [ init];
__strong Parent * strongFumu = fumu;
__weak Parent * weakFumu = fumu;

fumu = nil; //your auto variables window should show that both trongFumu and weakFumu are still valid with an address
NSLog(@&#34;weakFumu should be valid here, because strongFumu is still pointing to the object&#34;);

strongFumu = nil; //when strongFumu points away to nil, weakPtr will then also change to nil
NSLog(@&#34;weakFumu should be nil here&#34;);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>alloc</code> 是 allocate 的缩写,所以当你调用时<br/>
<code>Parent * fumu = [ init];</code><br/>
您分配对象,使其保留计数 =1 然后您调用 <code>;</code><br/>
您的对象保留计数高达 +2<br/>
那么当你调用 <code>;</code><br/>
它加了-1,所以你的最终计数将是+1,这是正确的。 </p>

<p>Strong 和 Weak 是 ARC 类型,您不能在非 ARC 项目中使用它们。它与属性/变量一起使用...当您需要拥有该对象时,您可能希望使用 <code>strong</code>,当您在示例中创建对象时,您已经“拥有”该对象.. . </p>

<p> <a href="https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1" rel="noreferrer noopener nofollow">https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Objective C 中的 __strong 用法示例,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32461808/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32461808/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Objective C 中的 __strong 用法示例