[英]Is there any difference between these 2 ways we create objects in ARC?
I am wondering is there actually any difference between:我想知道这两者之间实际上有什么区别:
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.deleteButton = deleteButton;
and:和:
self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
when using ARC ?何时使用 ARC ?
I see many people wrote a lot of code as in the first case, but I believe the second way is shorter and more clear and concise.我看到很多人像第一种情况一样写了很多代码,但我相信第二种方式更短,更清晰和简洁。
is there actually any difference between实际上有什么区别吗?
Yes, but, under compiler optimization, generated binary would be identical.是的,但是,在编译器优化下,生成的二进制文件将是相同的。
UIButton *deleteButton
, that is, UIButton __strong *deleteButton
has ownership of the object. UIButton *deleteButton
,即UIButton __strong *deleteButton
拥有对象的所有权。
The object was registered on the current Autorelease Pool because buttonWithType:
class method does not begin with "alloc", "new", "copy", or "mutableCopy".该对象已在当前自动释放池上注册,因为buttonWithType:
类方法不以“alloc”、“new”、“copy”或“mutableCopy”开头。
Thus,因此,
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
/*
* The current Autorelease Pool has ownership of the object. retainCount=1
* __strong deleteButton has ownership of the object as well. retainCount=2
*/
self.deleteButton = deleteButton;
/*
* If self.deleteButton is strong property, it has ownership of the object too. retainCount=3
* If self.deleteButton is weak property, it doesn't have ownership of the object. retainCount=2
*/
Not an ARC expert or anything here, but I believe the code is identical.不是 ARC 专家或这里的任何东西,但我相信代码是相同的。 When you assign to a pointer, you're assigning the memory address that the pointer points to.当您分配给一个指针时,您正在分配该指针指向的内存地址。 The only thing you're doing in the top one versus the bottom one is saving the memory address in a local pointer before assigning it to self.deleteButton.您在顶部与底部所做的唯一一件事是在将内存地址分配给 self.deleteButton 之前将其保存在本地指针中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.