2016-11-25 29 views
18

我有一点点混淆ARC引用计数可以请告诉我什么将引用计数波纹管代码。引用计数在ARC

var vc1 = UIViewController() 
var vc2 = vc1 
var vc3 = vc2 
weak var vc4 = vc3 

问题是什么将是:VC1的

  • 引用计数?
  • vc2的引用计数?
  • vc3的引用计数?
  • vc4的引用计数?

回答

15

这里,vc1,vc2,vc3涉及相同的对象。因此,该对象的引用计数为3.当vc4引用同一个对象时,由于它是弱引用,因此引用计数不会加1.因此,之后的引用计数也将为3

  1. 所创建和的第一行代码后vc1称为UIViewController对象的引用计数是1

    var vc1:UIViewController? = UIViewController() // strong reference 
    
  2. vc2是指相同的对象vc1后。对象的引用计数变为2

    var vc2:UIViewController? = vc1 // strong reference 
    
  3. vc3是指相同的对象vc1vc2后。对象的引用计数变为3

    var vc3:UIViewController? = vc2 // strong reference 
    
  4. vc4是指相同的对象vc1vc2vc3后。由于vc4是弱引用,引用计数不会增加。这意味着数仍为3.

    weak var vc4:UIViewController? = vc3 // weak reference 
    

这意味着什么:

执行下面的代码。

vc1 = nil; // reference count = 3-1 = 2 
    vc2 = nil; // reference count = 2-1 = 1 
    vc3 = nil; // reference count = 1-1 = 0 and object is destroyed 

现在,打印vc4的值。它将是nil。发生这种情况是因为对象的引用计数变为零,并且所有变量都引用同一个对象。

编辑:

在下面的代码中使用CFGetRetainCount给出如下说明如下结果:

var vc1:NSDate? = NSDate() 
print(CFGetRetainCount(vc1)) // 2 - I expected this to be 1 as only one variable is strongly referring this object. 

var vc2:NSDate? = vc1 
print(CFGetRetainCount(vc1)) // 3 - reference count incremented by 1 (strong reference) 

var vc3:NSDate? = vc2 
print(CFGetRetainCount(vc3)) // 4 - reference count incremented by 1 (strong reference) 

weak var vc4:NSDate? = vc1 
print(CFGetRetainCount(vc1)) // 4 - reference count not incremented (weak reference) 

vc1 = nil 
print(CFGetRetainCount(vc2)) // 3 - reference count decremented by 1 (strong reference removed) 

vc2 = nil 
print(CFGetRetainCount(vc3)) // 2 - reference count decremented by 1 (strong reference removed) 

vc3 = nil 
print(vc4) // nil - reference count should be decremented by 1 (last strong reference removed) 

// Also due to the final line vc3 = nil, reference count should become zero 
// However, we can't use `CFGetRetainCount` to get reference count in this case 
// This is due to the final strong reference being removed and object getting destroyed 

为什么CFRetainCount在一号线给2的原因进行了讨论here。感谢@CodaFi和@Sahil在评论中的讨论

+0

感谢KrishnaCA您惊人的响应 –

+0

不客气:) – KrishnaCA

+2

为什么CFGetRetainCount给4然后@KrishnaCA? – Sahil

1

1,2,3,4 - 引用计数为3

时引用计数不会递增唯一的例外 - 因为弱4号线,变质

2

在我看来VC1至VC3增加保留计数和通过默认属性是strong直到我们指定这些为weak

强:强是通常由一个类来建立一个对象的所有权。它增加了保留计数(ARC为你处理的东西),它基本上保留了内存中指向的对象,直到该类实例停止指向它为止。这通常是你想要的,但它可能会导致一种叫做

在VC4为你减速的情况下,weak“保留周期。”:

弱:这给一个指针对象,但不要求所有权,也不会增加保留计数。只要另一个类强烈指向它,它基本上保持一个有效的指向对象的指针。如果没有其他人试图保留它,弱指针会自动设置为零。

4

您可以使用CFGetRetainCount函数检查参考计数。

var vc1 = UIViewController() 
var vc2 = vc1 
var vc3 = vc2 
weak var vc4 = vc3 


print(CFGetRetainCount(vc1)) //4 
print(CFGetRetainCount(vc2)) //4 
print(CFGetRetainCount(vc3)) //4 
print(CFGetRetainCount(vc4)) //4 

你也可以参考这个Get Ref Count

+0

'CFGrtRetainCount'确实返回4,但你没有回答问题。看看CodaFi的评论,看看为什么你的答案是不正确的 – fpg1503

+0

@Sahil,尝试在这里每一步获得保留数并检查结果。我只是试了一下。 'var vc1 = UIViewController()'之后的'vc1'的保留计数是2.请确认 – KrishnaCA

+0

http://stackoverflow.com/questions/4636146/when-to-use-retaincount – Sahil

相关问题