使用alloc init代替new [英] Use of alloc init instead of new

查看:90
本文介绍了使用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天全站免登陆