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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:02:50  来源:igfitidea点击:

What is the difference between [Class new] and [[Class alloc] init] in iOS?

iosobjective-cnew-operatorallocationinit

提问by Prasad G

Possible Duplicate:
alloc, init, and new in Objective-C

可能的重复:
Objective-C 中的 alloc、init 和 new

I am a little confused about [Class new]and [[Class alloc] init]. I have defined an object contentusing [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 +newmethod is simply shorthand for +allocand -init. The ownership semantics are identical. The only benefit to using +newis that it is more concise. If you need to provide arguments to the class's initialiser, you will have to use the +allocand -initWith...methods instead.

+new方法只是+allocand 的简写-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 方法。