前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >alloc 、init、new详解

alloc 、init、new详解

作者头像
sweet说好的幸福
发布2020-12-23 09:52:15
9130
发布2020-12-23 09:52:15
举报
文章被收录于专栏:sweet_iOS

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

代码语言:javascript
复制
NSObject *obj = [NSObject alloc];
NSLog(@"%p",obj);
obj = [obj init];
NSLog(@"%p",obj);

打印结果:

NSObject.png

实验2:

代码语言:javascript
复制
NSString *name = [NSString alloc];
NSLog(@"%p",name);
name = [name init];
NSLog(@"%p",name);

打印结果:

NSString.png

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

代码语言:javascript
复制
- (id)init {
    if(self = [super init])  {// 重新赋值
        //…
    }
}

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

代码语言:javascript
复制
NSObject *obj = [NSObject alloc];
NSLog(@"%p",obj);
obj = [obj init];
NSLog(@"%p",obj);

打印结果为:

NSObjectSuper.png

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

alloc

看一下帮助文档:

alloc.jpeg

代码语言:javascript
复制
返回这个接受消息的类的一个实例.
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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协议时要覆写词方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • alloc
  • init
  • new
  • allocWithZone
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档