简体   繁体   English

我们在 ARC 中创建对象的这两种方式有什么区别吗?

[英]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.

相关问题 目标C中两种迭代方式之间的区别? - Difference between two ways of Iteration in Objective C? 以这两种方式实例化的约束之间有区别吗? - Is there a difference between the constraints instantiated in these two ways? ARC:成员变量和属性之间有什么区别? - ARC: what's the difference between a member variable and property? 当我们创建具有两个不同xcode版本的IPA文件时,ipa文件有什么区别吗? - When we create IPA files with two different xcode versions then is there any difference in ipa files? 如何添加阴影平面以及苹果提供和我们创建的.scn文件之间的区别 - How to add a shadow plane and the difference between the .scn file apple provide and we create 这两种在Objective-C中分配内存的方式有什么区别? - What is the difference between these two ways of allocating memory in Objective-C? 在UITableView的UITableViewCell下访问UIImageView的两种方法之间的区别 - The difference between two ways to access UIImageView under UITableViewCell of UITableView 导航到新的UIViewController的不同方式之间有什么区别? - What is the difference between the different ways to navigate to a new UIViewController? String和有什么不一样? 和弦! (创建可选变量的两种方式)? - What is the difference between String? and String! (two ways of creating an optional variable)? 设置按钮初始状态的两种方式之间的差异 - Difference between two ways of setting a button's initial state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM