这两行有什么区别?

发布于 2024-11-09 19:17:46 字数 191 浏览 0 评论 0原文

UIButton *btn=[[UIButton alloc] init]; 

这两个声明有什么

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

区别或者都是相同的?

UIButton *btn=[[UIButton alloc] init]; 

and

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

What is the difference between these two declarations or are both the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-11-16 19:17:46

第一个会将 UIButton 对象分配给 btn。您有责任在完成后释放它,因为您分配了内存。

第二个将执行相同的操作,但对象将自动释放,这意味着您不必显式调用release,因为操作系统将在必要时执行该操作。

注意:UIButtonType 也不同。

The first one will assign a UIButton object to btn. You are responsible for releasing it when you are finished, since you allocated the memory.

The second one will perform the same action, but the object will be autoreleased, meaning that you do not have to call release explicitly, as the operating system will perform that action when necessary.

Note: The UIButtonType is also different.

无言温柔 2024-11-16 19:17:46
[UIButton buttonWithType:...]

创建一个自动释放对象(仍然需要内存)。

[[UIButton alloc]init] 

创建一个不会自动释放的对象。你必须自己释放!

进一步查看这个问题

以及有关内存管理的更多信息。

[UIButton buttonWithType:...]

creates an autoreleased object (which still needs memory).

[[UIButton alloc]init] 

creates an object which is not going to be autoreleased. you have to release by yourself!

have a further look at this question.

And more about memory management.

心舞飞扬 2024-11-16 19:17:46

第一个为您提供了一个未自动释放的 UIButton,其 buttonTypeUIButtonTypeCustom

第二个为您提供了一个自动释放的 UIButton,其UIButtonTypeRoundedRectbuttonType

First one gives you a not autoreleased UIButton with a buttonType of UIButtonTypeCustom

Second one gives you an autoreleased UIButton with a buttonType of UIButtonTypeRoundedRect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文