Possible Duplicate:
alloc, init, and new in Objective-C可能的重复:alloc, init,以及Objective-C中的new
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]有点困惑。我已经使用[Class new]和[[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?
请解释一下两者的区别、优缺点。
3 个解决方案
#1
124
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
分配和初始化
new = alloc + init
新= alloc + init
#2
26
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.
+新方法是+alloc和-init的简写。所有权语义是相同的。使用+new的唯一好处是它更简洁。如果需要向类的初始化器提供参数,则必须使用+alloc和-initWith…而不是方法。
#3
9
Here: alloc, init, and new in Objective-C
这里是alloc init和Objective-C中的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方法。
#1
124
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
分配和初始化
new = alloc + init
新= alloc + init
#2
26
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.
+新方法是+alloc和-init的简写。所有权语义是相同的。使用+new的唯一好处是它更简洁。如果需要向类的初始化器提供参数,则必须使用+alloc和-initWith…而不是方法。
#3
9
Here: alloc, init, and new in Objective-C
这里是alloc init和Objective-C中的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方法。