object c中 new和alloc区别

在object-c基础教程这本书里老是可以看见类似下面这种代码,我这小菜鸟就纳闷了,啥区别啊,看上去都一样么。网上找了找,找到一些说法,这里先摘出来。
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值