ios iOS中的[Class new]和[[Class alloc] init]有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11256228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between [Class new] and [[Class alloc] init] in iOS?
提问by Prasad G
Possible Duplicate:
alloc, init, and new in Objective-C
I am a little confused about [Class new]
and [[Class alloc] init]
. I have defined an object content
using [Class new]
and [[Class alloc] init]
.
我对[Class new]
和 有点困惑[[Class alloc] init]
。我已经content
使用[Class new]
and 定义了一个对象[[Class alloc] init]
。
(1). NSMutableArray *content = [NSMutableArray new];
(2). NSMutableArray *content = [[NSMutableArray alloc] init];
My question is about the differences between [Class new]
and [[Class alloc] init]
. For me, (1) and (2) are similar. If (1) and (2) are similar, then why do we use [[Class alloc] init]
most of the time, compared to [Class new]
? I think that there must be some difference.
我的问题是关于之间的差异[Class new]
和[[Class alloc] init]
。对我来说,(1)和(2)是相似的。如果 (1) 和 (2) 相似,那么与 相比,为什么我们[[Class alloc] init]
大部分时间都使用[Class new]
?我认为一定有一些区别。
Kindly explain the differences, pros & cons of both?
请解释两者的区别,优缺点?
回答by Shantanu
Alloc:Class method of NSObject. Returns a new instance of the receiving class.
Alloc:NSObject 的类方法。返回接收类的新实例。
Init: Instance method of NSObject. Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
Init: NSObject 的实例方法。由子类实现以在为其分配内存后立即初始化新对象(接收器)。
New: Class method of NSObject. Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.
新:NSObject 的类方法。分配接收类的新实例,向它发送一个 init 消息,并返回初始化的对象。
Release: Instance method of NSObject delegate. Decrements the receiver's reference count.
Release: NSObject 委托的实例方法。减少接收者的引用计数。
Autorelease: Instance method of NSObject delegate. Adds the receiver to the current autorelease pool.
Autorelease: NSObject 委托的实例方法。将接收器添加到当前的自动释放池。
Retain:Instance method of NSObject delegate. Increments the receiver's reference count.
Retain:NSObject 委托的实例方法。增加接收者的引用计数。
Copy:Instance method of NSObject delegate. Returns a new instance that's a copy of the receiver.
复制:NSObject 委托的实例方法。返回一个新实例,它是接收器的副本。
So to conclude we can say that
所以总而言之,我们可以说
alloc goes with init
alloc 与 init 一起使用
new = alloc + init
新 = 分配 + 初始化
回答by dreamlax
The +new
method is simply shorthand for +alloc
and -init
. The ownership semantics are identical. The only benefit to using +new
is that it is more concise. If you need to provide arguments to the class's initialiser, you will have to use the +alloc
and -initWith...
methods instead.
该+new
方法只是+alloc
and 的简写-init
。所有权语义是相同的。使用的唯一好处+new
是它更简洁。如果需要为类的初始化程序提供参数,则必须改用+alloc
和-initWith...
方法。
回答by Cehh?ro
Here: alloc, init, and new in Objective-C
此处:Objective-C 中的 alloc、init 和 new
Basically it's a question of modern versus traditional. The most direct advantage of init over new is that there are many custom init methods.
基本上这是一个现代与传统的问题。init 相对于 new 最直接的优点是有很多自定义的 init 方法。