alloc 、init、new详解

先做两个实验,猜测打印结果:
实验1:

NSObject *obj = [NSObject alloc];
NSLog(@"%p",obj);
obj = [obj init];
NSLog(@"%p",obj);

打印结果:


NSObject.png

实验2:

NSString *name = [NSString alloc];
NSLog(@"%p",name);
name = [name init];
NSLog(@"%p",name);

打印结果:


NSString.png

实际上,将NSString换成其他的类型,例如:NSArray、NSDictionary等等,会发现结果同NSString。
为什么NSString的两次内存地址不一样?基础好的同学可能猜测到原因了。
首先,NSObject是所有类的根类,申明了init方法,看一下NSString的init方法:

- (id)init {
    if(self = [super init])  {// 重新赋值
        //…
    }
}

也就是说NSString在使用init初始化时,会调用[super init],如果不为nil,就重新分配内存空间,导致内存空间不一致,而NSObject就是根类,不存在superClass,自然内存空间是一样的。为了验证这点我们再跑一篇程序:
实验1升级版:

NSObject *obj = [NSObject alloc];
NSLog(@"%p",obj);
obj = [obj init];
NSLog(@"%p",obj);

打印结果为:


NSObjectSuper.png

跟我们想象的一样,superClass的内存为0x0。同样,在打印一次NSString,会发现NSString的对象在init后和superClass的地址是一样的。

alloc

看一下帮助文档:


alloc.jpeg
返回这个接受消息的类的一个实例.
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.
这个实例初始化后可以用来表示这个类的数据相关的结构;所有其他的实例变量的值都被设置成 0.
You must use an init... method to complete the initialization process. For example:
你必须使用 init... 方法来最终完成这个初始化的步骤,如下:
TheClass *newObject = [[TheClass alloc] init];
Do not override alloc to include initialization code. Instead, implement class-specific versions of init... methods.
不要重写 alloc 来包含初始化的代码.你可以使用指定版本的 init... 方法来达到你的目的.
For historical reasons, alloc invokes allocWithZone:.
由于历史原因,allc 方法调用了 allocWithZone: 方法.

结论:

  • alloc 后只是在系统中分配了内存,这段内存空间的大小与这个类本身结构所占字节的大小相等,并返回了这段内存的指针.
  • alloc 将申请内存空间中的值都设置为 0.
  • alloc 调用了方法 allocWithZone:.
  • alloc 就执行了一次,没有继承的关系.

init

看一下帮助文档:


init.jpeg
Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
子类实现初始化一个刚刚获取到内存空间的对象.
An init message is coupled with an alloc (or allocWithZone:) message in the same line of code:
init 消息与 alloc (或者是 allocWithZone:) 消息在一行内执行:
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.
对象只有执行了 init 方法后才能够被使用. NSObject 类定义了这个初始化方法,但是 NSObject 并没有初始化什么,它直接将指针返回了.
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. For example, a hypothetical BuiltInCamera class might return nil from its init method if run on a device that has no camera.
如果要自定义这个实现方法.你必须调用 super 来先初始化父类的对象.如果这个新对象不能被初始化,这个方法就应该返回 nil.例如,假设有一个照相机相关的类,如果在一个没有照相机的设备上调用了这个类,那么,在父类的 init 方法中就会返回 nil.

结论:

  • 重写 init 方法时需要先初始化父类的 init 方法.
  • NSObject 中的 init 方法什么也没做,只是返回了自己而已.
  • 如果初始化失败,会返回 nil.

new

帮助文档:


new.jpeg
Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.
分配一个类的新实例内存地址,并执行init方法,返回这个初始化得对象。
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.
这个方法可以当做alloc和init的组合。首先alloc初始化了一个class结构体对象的指针,然后调用init方法完成初始化过程。

结论:

  • new 可以看做是alloc与init的组合.
  • 当不采用init或者自定义初始化方法时,不适合用new

allocWithZone

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.

调用alloc会默认调用allocWithZone。在申明对象支持copy协议时要覆写词方法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,814评论 0 9
  • 1.1 什么是自动引用计数 概念:在 LLVM 编译器中设置 ARC(Automaitc Reference Co...
    __silhouette阅读 5,271评论 1 17
  • 副标题:生活家~My love 以下排名不分先后 画画(手绘,电脑):服饰,场景,室内外景观,人物,幻想。 摄影;...
    玉妖凰儿阅读 255评论 0 1
  • 昨天半夜收到允翰的邀请,去模拟群面现场当一把面试官。说实话,也挺想从面试官的角度感受一下群面现场的。于是,自己挖坑...
    DrEthan阅读 436评论 1 1
  • 想不通的 不只别人 还有自己 我越来越不了解 任性或者情绪 爱或者表达 理性以及冲突 想 打破了 揉碎了 重新捏一...
    西米梦露阅读 264评论 0 1