alloc init和new in Objective-C [duplicate]

时间:2022-09-07 11:25:15

This question already has an answer here:

这个问题已经有了答案:

One book for about iPhone programming instantiates classes like this:

关于iPhone编程的一本书实例化了这样的类:

[[Class alloc] init]

Another book about Objective-C does it like this:

另一本关于Objective-C的书是这样写的:

[Class new]

What's the difference?

有什么区别呢?

4 个解决方案

#1


70  

Originally in Objective-C, objects were created with new. As the OpenStep/Cocoa framework evolved, the designers developed the opinion that allocating the memory for an object and initializing its attributes were separate concerns and thus should be separate methods (for example, an object might be allocated in a specific memory zone). So the alloc-init style of object creation came into favor.

最初在Objective-C中,对象是用new创建的。随着OpenStep/Cocoa框架的发展,设计人员开发了一种观点,即为对象分配内存并初始化其属性是独立的关注点,因此应该是单独的方法(例如,对象可能会在特定的内存区域中分配)。因此,对象创建的分配-init样式开始流行起来。

Basically, new is old and almost-but-not-quite deprecated — thus you'll see that Cocoa classes have a lot of init methods but almost never any custom new methods.

基本上,new是旧的,但几乎没有被弃用——因此您将看到Cocoa类有很多init方法,但几乎没有任何自定义的新方法。

#2


72  

+new is implemented quite literally as:

+new的字面意思是:

+ (id) new
{
    return [[self alloc] init];
}

Nothing more, nothing less. Classes might override it, but that is highly atypical in favor of doing something like +fooWithBar:.

不多不少。类可能会覆盖它,但这是非常不典型的做法,比如使用+fooWithBar:。

#3


13  

As already mentioned, by defaut there is no difference. But you can overwrite the new class method. Apple's documentation has some thoughts on this.

正如前面提到过的,通过defaut,没有区别。但是您可以重写新的类方法。苹果的文档对此有一些想法。

Unlike alloc, new is sometimes re-implemented in subclasses to invoke a class-specific initialization method[...] Often new... methods will do more than just allocation and initialization.

与alloc不同的是,有时在子类中重新实现新实现,以调用类特定的初始化方法[…通常新…方法不仅仅是分配和初始化。

#4


2  

It depends on the Class, but [Class new] is most likely a convenience method that calls [[Class alloc] init] internally. Thus, you can not call other init methods such as "initWithString".

它取决于类,但[Class new]很可能是在内部调用[[Class alloc] init]的一种方便方法。因此,您不能调用其他init方法,比如“initWithString”。

#1


70  

Originally in Objective-C, objects were created with new. As the OpenStep/Cocoa framework evolved, the designers developed the opinion that allocating the memory for an object and initializing its attributes were separate concerns and thus should be separate methods (for example, an object might be allocated in a specific memory zone). So the alloc-init style of object creation came into favor.

最初在Objective-C中,对象是用new创建的。随着OpenStep/Cocoa框架的发展,设计人员开发了一种观点,即为对象分配内存并初始化其属性是独立的关注点,因此应该是单独的方法(例如,对象可能会在特定的内存区域中分配)。因此,对象创建的分配-init样式开始流行起来。

Basically, new is old and almost-but-not-quite deprecated — thus you'll see that Cocoa classes have a lot of init methods but almost never any custom new methods.

基本上,new是旧的,但几乎没有被弃用——因此您将看到Cocoa类有很多init方法,但几乎没有任何自定义的新方法。

#2


72  

+new is implemented quite literally as:

+new的字面意思是:

+ (id) new
{
    return [[self alloc] init];
}

Nothing more, nothing less. Classes might override it, but that is highly atypical in favor of doing something like +fooWithBar:.

不多不少。类可能会覆盖它,但这是非常不典型的做法,比如使用+fooWithBar:。

#3


13  

As already mentioned, by defaut there is no difference. But you can overwrite the new class method. Apple's documentation has some thoughts on this.

正如前面提到过的,通过defaut,没有区别。但是您可以重写新的类方法。苹果的文档对此有一些想法。

Unlike alloc, new is sometimes re-implemented in subclasses to invoke a class-specific initialization method[...] Often new... methods will do more than just allocation and initialization.

与alloc不同的是,有时在子类中重新实现新实现,以调用类特定的初始化方法[…通常新…方法不仅仅是分配和初始化。

#4


2  

It depends on the Class, but [Class new] is most likely a convenience method that calls [[Class alloc] init] internally. Thus, you can not call other init methods such as "initWithString".

它取决于类,但[Class new]很可能是在内部调用[[Class alloc] init]的一种方便方法。因此,您不能调用其他init方法,比如“initWithString”。