Objective -C Object initialization 对象初始化
Objective -C Object initialization 对象初始化
1.1 Allocating Objects 分配对象
Allocation is the process by which a new object is born.
allocation 是新对象诞生的过程。
Sending the alloc message to a class causes that class to allocate a chunk of memory large enough to hold all its instance variables.
发送alloc 消息能够让class分配足够多得内存。
All your BOOLs start out as NO;
all your ints are 0; all your floats become 0.0; all your pointers are nil;
A newly allocated object isn't ready to be used right away: you need to initialize it before you can work with it.
一个刚分配的对象还不能用,你需要初始化它。
Some languages, including C++ and Java, perform object allocation and initialization in a single operation using a constructor. Objective-C splits the two operations into explicit allocation and initialization stages.
java和C++执行allocation and initialization 在一个操作里。
而Objective-C分开了两个操作。
1.2 Initializing objects 对象初始化
Initialization takes a chunk of memory and gets it ready to become a productive member of society. init methods—that is, methods that do initialization—almost always return the object they're initializing.
初始化让其成为对象的一员。init methods 也就是说总是返回要被创建的对象。
Car *car = [[Car alloc] init];
1.3 Writing initializationg methods 写初始化方法
(id) init {
if (self = [super init])
{
engine = [Engine new];
tires[0] = [Tire new];
tires[1] = [Tire new];
tires[2] = [Tire new];
tires[3] = [Tire new];
}
return (self);
} // init
The first bit of code that runs in that statement is [super init].That code lets the superclass do its initialization work.
[super init]允许父类完成初始化工作。
For classes that inherit from NSObject, calling on the superclass lets NSObject do any processing it needs to do so that objects can respond to messages and deal with retain counts.
对于任何继承自NSObject的类,初始化能够让对象相应详细和处理retain counts .
For classes that inherit from another class, this is their chance to do their own version of clean-slate initialization.
对于继承自其他类的类,它有机会初始化自身。
Remember that instance variables are found at a memory location that's a fixed distance from the hidden self parameter.
记住在实例变量发现在内存的位置,固定位置能够找到隐藏的self参数。
If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory.
如果一个对象返回一个init 方法,我们需要更新self 才能在任何子变量中适当的位置找到实例变量。
An init method can return nil if there's a problem initializing an object.
一个初始化方法可能返回nil 。
Keep in mind that this assignment affects the value of self only for this method. It doesn't change anything outside the method's scope.
记住这个分配只影响self 在本方法内。不会影响其他方法。
The test if (self = [super init]) won't run the body code if nil is returned from [super init].
这个测试不会运行,如果[super init ]返回为nil的话。
An init method returns the object that was just initialized. Since we assigned the return value of [super init] to self, that's what we should return.
一个正确地初始化方法返回一个初始化对象。因为我们把[super init ]赋值给self ,所以返回self
1.4 What to Do When You're Initializing
This is the place to do your clean-slate initialization work. You assign values to instance variables and create the other objects that your object needs to do its work.
在这里,你可以做一些从头再来的工作。你分配值和创建你需要的其他对象.初始化需要做什么工作还是看你自己的需求。
1.5 Convenience initializers
In fact, it's important to remember that init methods are nothing special. They're just ordinary methods that follow a naming convention.
init methods没什么特殊的。他们也是普通的类。
Many classes have convenience initializers. These are init methods that do some extra work, saving you the trouble of doing it yourself.
许多类提供简单方法。做额外的工作。
- (id) initWithFormat: (NSString *) format, ...;
这个也是初始化方法。
Objective -C Object initialization 对象初始化的更多相关文章
- prototype linkage can reduce object initialization time and memory consumption
//对象是可变的键控集合, //"numbers, strings, booleans (true and false), null, and undefined" 不是对象的解释 ...
- C++中的对象初始化
当对象在创建时获得了一个特定的值,我们说这个对象被初始化.初始化不是赋值,初始化的含义是创建变量赋予其一个初始值,而赋值的含义是把当前值擦除,而以一个新值来替代.对象初始化可以分为默认初始化.直接初始 ...
- C++中对象初始化
在C++中对象要在使用前初始化,永远在使用对象之前先将它初始化. 1.对于无任何成员的内置类型,必须手工完成此事. 例如: int x=0; double d; std::cin>>d; ...
- Java对象初始化详解
在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.本文试图对Java如何执行对象的初始化做一个详细深入地介绍(与对象初始化相同,类在被加载之后也是需要初始化的,本 ...
- Java对象初始化详解(转)
在Java中,一个对象在可以被使用之前必须要被正确地初始化,这一点是Java规范规定的.本文试图对Java如何执行对象的初始化做一个详细深入地介绍(与对象初始化相同,类在被加载之后也是需要初始化的,本 ...
- C#语法糖之第二篇: 参数默认值和命名参数 对象初始化器与集合初始化器
今天继续写上一篇文章C#4.0语法糖之第二篇,在开始今天的文章之前感谢各位园友的支持,通过昨天写的文章,今天有很多园友们也提出了文章中的一些不足,再次感谢这些关心我的园友,在以后些文章的过程中不断的完 ...
- 第七节:语法总结(1)(自动属性、out参数、对象初始化器、var和dynamic等)
一. 语法糖简介 语法糖也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方 ...
- jQuery对象初始化的多种传参数形式
jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...
- {{jQuery源码分析}}jQuery对象初始化的多种传参数形式
jQuery对象初始化的传参方式包括:1.$(DOMElement)2.$('<h1>...</h1>'), $('#id'), $('.class') 传入字符串, 这是最常 ...
随机推荐
- github远程仓储和本地仓储进行关联
我们使用github作为远程仓储,需要提前注册号一个github账号 由于github仓储和本地仓储之间传输是使用ssh加密的,所以需要一点设置, 1.创建sshkey gitbash执行: ssh ...
- 下面forward和redirect的描述,正确的是(ABCD)
A:forward是服务器将控制权转交给内部服务器对象,由新的对象来全权负责响应用户的请求 B:执行forward时,浏览器不知道服务器所发送的内容从那里来,浏览器地址栏中还是原来的地址 C:执行re ...
- Local Databases with SQLiteOpenHelper
Overview For maximum control over local data, developers can use SQLite directly by leveraging SQLit ...
- python 闭包变量不允许write,要使用nonlocal
以下是一段简单的闭包代码示例: def foo(): m=3 n=5 def bar(): a=4 return m+n+a return bar >>>bar = foo() &g ...
- 字节流与字符流简单操作(OutputStream、InputStream、Writer、Reader)
操作流程 使用File类打开一个文件 通过字节流或者字符流的子类.指定输出的位置. 进行读/写操作 关闭输入/出 字节流与字符流 在java.io包中操作文件内容主要有两大类:字节流字符流.两大类分为 ...
- Mysql数据库基础操作
Mysql数据库基础操作 在mysql数据库中开启使用tab键补全功能 1)修改主配置文件/etc/mysql/my.cnf(mysql和mariadb目录有些不同) vim /etc/mysql/m ...
- JS Promise API
一.描述 我们知道JavaScript语言的执行环境是“单线程”,所谓单线程,就是一次只能够执行一个任务,如果有多个任务的话就要排队,前面一个任务完成后才可以继续下一个任务. 这种“单线程”的好处就是 ...
- sql server 分组排序
环境: sql server 2012 语法 select ROW_NUMBER() over(partition BY 分组字段 order by 排序字段),* as rowNums from 表 ...
- Codeforces Round #209 (Div. 2) C - Prime Number
传送门 题意 给出n个数及x,求 \[\frac{\sum _{i=1}^n x^{a_1+a_2+...+a_{i-1}+a_{i+1}+...a_n}}{\prod_{i=1}^n x^{a_i} ...
- HDU-ACM“菜鸟先飞”冬训系列赛——第10场
Problem A 题意 给出l(房子宽度),d(pole距离房子的垂直距离),s(绳子长度),求可覆盖的面积 分析 一共四种情况 \[1.s<=d\] \[2.s<=sqrt(d*d+l ...