objective-c 使用 alloc init 而不是 new

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/719877/
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-09-03 21:21:36  来源:igfitidea点击:

Use of alloc init instead of new

objective-coop

提问by willc2

Learning Objective-C and reading sample code, I notice that objects are usually created using this method:

学习Objective-C并阅读示例代码,我注意到通常使用这种方法创建对象:

SomeObject *myObject = [[SomeObject alloc] init];

instead of:

代替:

SomeObject *myObject = [SomeObject new];

Is there a reason for this, as I have read that they are equivalent?

是否有原因,因为我已经读到它们是等效的?

采纳答案by Jeremy Stanley

There are a bunch of reasons here: http://macresearch.org/difference-between-alloc-init-and-new

这里有很多原因:http: //macresearch.org/difference-between-alloc-init-and-new

Some selected ones are:

一些选择的是:

  • newdoesn't support custom initializers (like initWithString)
  • alloc-initis more explicit than new
  • new不支持自定义初始值设定项(如initWithString
  • alloc-initnew

General opinion seems to be that you should use whatever you're comfortable with.

一般意见似乎是你应该使用任何你觉得舒服的东西。

回答by guitar_freak

Very old question, but I've written some example just for fun — maybe you'll find it useful ;)

很老的问题,但我写了一些例子只是为了好玩——也许你会发现它很有用;)

#import "InitAllocNewTest.h"

@implementation InitAllocNewTest

+(id)alloc{
    NSLog(@"Allocating...");
    return [super alloc];
}

-(id)init{
    NSLog(@"Initializing...");
    return [super init];
}

@end

In main function both statements:

在主函数中,两个语句:

[[InitAllocNewTest alloc] init];

and

[InitAllocNewTest new];

result in the same output:

导致相同的输出:

2013-03-06 16:45:44.125 XMLTest[18370:207] Allocating...
2013-03-06 16:45:44.128 XMLTest[18370:207] Initializing...
2013-03-06 16:45:44.125 XMLTest[18370:207] Allocating...
2013-03-06 16:45:44.128 XMLTest[18370:207] Initializing...

回答by Barry Wark

+newis equivalent to +alloc/-initin Apple's NSObjectimplementation. It is highly unlikely that this will ever change, but depending on your paranoia level, Apple's documentation for +newappears to allow for a change of implementation (and breaking the equivalency) in the future. For this reason, because "explicit is better than implicit" and for historical continuity, the Objective-C community generally avoids +new. You can, however, usually spot the recent Java comers to Objective-C by their dogged use of +new.

+new相当于+alloc/-init在苹果的NSObject实现中。这不太可能会改变,但根据您的偏执程度,Apple 的文档+new似乎允许将来更改实施(并打破等效性)。出于这个原因,因为“显式优于隐式”以及为了历史的连续性,Objective-C 社区通常避免+new. 但是,您通常可以通过他们顽固地使用+new.

回答by Brian Campbell

Frequently, you are going to need to pass arguments to initand so you will be using a different method, such as [[SomeObject alloc] initWithString: @"Foo"]. If you're used to writing this, you get in the habit of doing it this way and so [[SomeObject alloc] init]may come more naturally that [SomeObject new].

通常,您需要将参数传递给init,因此您将使用不同的方法,例如[[SomeObject alloc] initWithString: @"Foo"]. 如果你习惯写这个,你就会养成这样写的习惯,所以[[SomeObject alloc] init]可能会更自然地使用[SomeObject new].

回答by user739711

One Short Answere is:

一个简短的回答是:

  1. Both are same. But
  2. 'new' only works with the basic 'init' initializer, and will not work with other initializers (eg initWithString:).
  1. 两者都是一样的。但
  2. 'new' 仅适用于基本的 'init' 初始化程序,不能与其他初始化程序(例如 initWithString:)一起使用。

回答by MurderDev

I am very late to this but I want to mention that that new is actually unsafe in the Obj-C with Swift world. Swift will only create a default init method if you do not create any other initializer. Calling new on a swift class with a custom initializer will cause a crash. If you use alloc/init then the compiler will properly complain that init does not exist.

我对此很晚,但我想提一下,在 Swift 世界的 Obj-C 中,新的实际上是不安全的。如果您不创建任何其他初始化程序,Swift 只会创建一个默认的 init 方法。使用自定义初始值设定项在 swift 类上调用 new 将导致崩溃。如果您使用 alloc/init,那么编译器会正确地抱怨 init 不存在。

回答by evdude100

For a side note, I personally use [Foo new]if I want something in init to be done without using it's return value anywhere. If you do not use the return of [[Foo alloc] init]anywhere then you will get a warning. More or less, I use [Foo new]for eye candy.

附带说明一下,[Foo new]如果我希望在 init 中完成某些操作而不在任何地方使用它的返回值,我个人会使用它。如果您不使用[[Foo alloc] init]任何地方的返回,那么您将收到警告。或多或少,我[Foo new]用于眼睛糖果。

回答by Mike Crawford

If new does the job for you, then it will make your code modestly smaller as well. If you would otherwise call [[SomeClass alloc] init]in many different places in your code, you will create a Hot Spot in new's implementation - that is, in the objc runtime - that will reduce the number of your cache misses.

如果 new 为您完成了这项工作,那么它也会使您的代码小一些。如果您要[[SomeClass alloc] init]在代码中的许多不同位置调用,您将在 new 的实现中创建一个热点 - 即在 objc 运行时 - 这将减少缓存未命中的数量。

In my understanding, if you need to use a custom initializer use [[SomeClass alloc] initCustom].

据我了解,如果您需要使用自定义初始值设定项,请使用[[SomeClass alloc] initCustom].

If you don't, use [SomeClass new].

如果没有,请使用[SomeClass new].