在object-c基础教程这本书里老是可以看见类似下面这种代码,我这小菜鸟就纳闷了,啥区别啊,看上去都一样么。网上找了找,找到一些说法,这里先摘出来。
className* object = [className new];
or
className* object = [[className alloc] init];
背景说明,new是较为老式的写法,后来发现只有一个new不好使,才引入了alloc和init这种写法,保留new一是向后兼容,二是很多时候是一种更简单的写法。其实是一样的,new在内部调用的alloc和init.
源代码:
[className new]基本等同于[[className alloc] init]. 区别只在于alloc分配内存的时候使用了zone,这个zone是个什么东东呢?它是给对象分配内存的时候,把关联的对象分配到一个相邻的内存区域内,以便于调用时消耗很少的代价,提升了程序处理速度.
什么要把alloc 和init 分开?
1. 可以使用多种init方法
2. 显示调用总比隐式调用要好”
如果确实不需要用其他的init函数,比如initWithString, 只是使用 [Nsobject alloc] init] ,那用new的方法更加方便
* new doesn't support custom initializers (like initWithString)
* alloc-init is more explicit than new
className* object = [className new];
or
className* object = [[className alloc] init];
背景说明,new是较为老式的写法,后来发现只有一个new不好使,才引入了alloc和init这种写法,保留new一是向后兼容,二是很多时候是一种更简单的写法。其实是一样的,new在内部调用的alloc和init.
源代码:
<p></p><p style="text-indent: 2em;"><span class="pun">+</span><span class="pln"> </span><span class="kwd">new</span> <span class="pun">{</span> <span class="pln">id newObject </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(*</span><span class="pln">_alloc</span><span class="pun">)((</span><span class="typ">Class</span><span class="pun">)</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">);</span> <span class="typ">Class</span><span class="pln"> metaClass </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">-></span><span class="pln">isa</span><span class="pun">;</span> <span class="kwd">if</span><span class="pln"> (class_getVersion</span><span class="pun">(</span><span class="pln">metaClass</span><span class="pun">)</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="lit">1</span><span class="pun">)</span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> [newObject init</span><span class="pun">];</span> <span class="kwd">else</span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> newObject</span><span class="pun">;</span> <span class="pun">}</span></p><p style="text-indent: 2em;"><span class="pun">+</span><span class="pln"> alloc</span> <span class="pun">{</span> <span class="kwd">return</span><span class="pln"> (*_zoneAlloc</span><span class="pun">)((</span><span class="typ">Class</span><span class="pun">)</span><span class="kwd">self</span><span class="pun">,</span><span class="pln"> </span><span class="lit">0</span><span class="pun">,</span><span class="pln"> malloc_default_zone</span><span class="pun">()); </span> <span class="pun">}</span></p><p style="text-indent: 2em;"><span class="pun">-</span><span class="pln"> init</span> <span class="pun">{</span> <span class="pln"> </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">self</span><span class="pun">;</span> <span class="pun">}</span></p><p></p>
[className new]基本等同于[[className alloc] init]. 区别只在于alloc分配内存的时候使用了zone,这个zone是个什么东东呢?它是给对象分配内存的时候,把关联的对象分配到一个相邻的内存区域内,以便于调用时消耗很少的代价,提升了程序处理速度.
什么要把alloc 和init 分开?
1. 可以使用多种init方法
2. 显示调用总比隐式调用要好”
如果确实不需要用其他的init函数,比如initWithString, 只是使用 [Nsobject alloc] init] ,那用new的方法更加方便
* new doesn't support custom initializers (like initWithString)
* alloc-init is more explicit than new