使用 alloc init 而不是 new [英] Use of alloc init instead of new

查看:27
本文介绍了使用 alloc init 而不是 new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Objective-C并阅读示例代码,我注意到通常使用这种方法创建对象:

Learning Objective-C and reading sample code, I notice that objects are usually created using this method:

SomeObject *myObject = [[SomeObject alloc] init];

代替:

SomeObject *myObject = [SomeObject new];

这是否有原因,因为我已经读到它们是等效的?

Is there a reason for this, as I have read that they are equivalent?

推荐答案

这里有很多原因:http://macresearch.org/difference-between-alloc-init-and-new

部分选定的是:

  • new 不支持自定义初始值设定项(例如 initWithString)
  • alloc-initnew
  • 更明确
  • new doesn't support custom initializers (like initWithString)
  • alloc-init is more explicit than new

一般的意见似乎是你应该使用任何你觉得舒服的东西.

General opinion seems to be that you should use whatever you're comfortable with.

这篇关于使用 alloc init 而不是 new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆