[[NSObject alloc] init] 与 [NSObject new] 的区别

来源:互联网 发布:尤克里里谱软件下载 编辑:程序博客网 时间:2024/06/05 22:52

背景说明:


new

Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.

+ (id)new
Return Value

A new instance of the receiver.

Discussion

This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the initmethod to complete the initialization process.

Availability
  • Available in OS X v10.0 and later.

alloc

Returns a new instance of the receiving class.

+ (id)alloc
Return Value

A new instance of the receiver.

Discussion

The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0.

You must use an init... method to complete the initialization process. For example:

TheClass *newObject = [[TheClass alloc] init];

Do not override alloc to include initialization code. Instead, implement class-specific versions of init... methods.

For historical reasons, alloc invokes allocWithZone:.

Availability
  • Available in OS X v10.0 and later.


allocWithZone:

Returns a new instance of the receiving class.

+ (id)allocWithZone:(NSZone *)zone
Parameters
zone

This parameter is ignored.

Return Value

A new instance of the receiver.

Discussion

The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0.

You must use an init... method to complete the initialization process. For example:

TheClass *newObject = [[TheClass allocWithZone:nil] init];

Do not override allocWithZone: to include any initialization code. Instead, class-specific versions of init... methods.

This method exists for historical reasons; memory zones are no longer used by Objective-C.

Availability
  • Available in OS X v10.0 and later.

init

Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

- (id)init
Return Value

An initialized object.

Discussion

An init message is coupled with an alloc (or allocWithZone:) message in the same line of code:

TheClass *newObject = [[TheClass alloc] init];

An object isn’t ready to be used until it has been initialized. The init method defined in the NSObject class does no initialization; it simply returns self.

In a custom implementation of this method, you must invoke super’s designated initializer then initialize and return the new object. If the new object can’t be initialized, the method should return nil.

- (id)init {
    self = [super init];
    if (self) {
        // Initialize self.
    }
    return self;
}

In some cases, an init method might return a substitute object. You must therefore always use the object returned by init, and not the one returned by alloc orallocWithZone:, in subsequent code.

Availability
  • Available in OS X v10.0 and later.


总结:


new doesn't support custom initializers (like initWithString)
alloc-init is more explicit than new


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


参考:


http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html

https://student.brighton.ac.uk/burks/language/objc/iaq/q3.htm

http://macresearch.org/difference-between-alloc-init-and-new