[[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.
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 init
method to complete the initialization process.
Availability
- Available in OS X v10.0 and later.
alloc
Returns a new instance of the receiving class.
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.
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.
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
- [[NSObject alloc] init] 与 [NSObject new] 的区别
- new 与 alloc/init的区别
- new 与 alloc/init的区别
- new 与 alloc/init的区别
- iOS new与alloc/init的区别
- id 与NSObject的区别
- alloc init new的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- new与alloc init,[NSArray array] 和 [[NSArray alloc]init] 及 self. 和 _ 的区别
- iOS开发:new 与alloc/init 的区别
- iOS开发new与alloc/init的区别
- iOS开发 new与alloc/init的区别
- iOS开发new与alloc/init的区别
- RTTI运行阶段类型识别(Runtime Type Identification)
- 【Java】JDK1.7的新特性。
- Linux安装Python的方法
- NYOJ 亡命逃窜
- oracle scott 用户锁定
- [[NSObject alloc] init] 与 [NSObject new] 的区别
- Android 短信模块分析(六) 接收Wappush信息及彩信附件增加删除
- Android---App Widget(二)
- NYOJ 幸运三角形
- 传智播客Java自学笔记第12天(待编辑)
- 传智播客Java自学笔记第13天(待编辑)
- CMake 支持中文
- Android 短信模块分析(七) MMS数据库定义及结构整理
- 传智播客Java自学笔记第14天(待编辑)