这两行有什么区别?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个会将
UIButton
对象分配给btn
。您有责任在完成后释放它,因为您分配了
内存。第二个将执行相同的操作,但对象将自动释放,这意味着您不必显式调用release,因为操作系统将在必要时执行该操作。
注意:
UIButtonType
也不同。The first one will assign a
UIButton
object tobtn
. You are responsible for releasing it when you are finished, since youalloc
ated 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.创建一个自动释放对象(仍然需要内存)。
创建一个不会自动释放的对象。你必须自己释放!
进一步查看这个问题。
以及有关内存管理的更多信息。
creates an autoreleased object (which still needs memory).
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.
第一个为您提供了一个未自动释放的
UIButton
,其buttonType
为UIButtonTypeCustom
第二个为您提供了一个自动释放的
UIButton
,其UIButtonTypeRoundedRect
的buttonType
First one gives you a not autoreleased
UIButton
with abuttonType
ofUIButtonTypeCustom
Second one gives you an autoreleased
UIButton
with abuttonType
ofUIButtonTypeRoundedRect