alloc和new的區(qū)別

在NSObject.mm文件中可以找到alloc方法和new方法的實(shí)現(xiàn)

***如果使用new的話,初始化方法就被固定調(diào)用init***
+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}

+ (id)alloc {
    return _objc_rootAlloc(self);
}

// Replaced by ObjectAlloc
+ (id)allocWithZone:(struct _NSZone *)zone {
    return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone);
}

// Base class implementation of +alloc. cls is not nil.
// Calls [cls allocWithZone:nil].
id
_objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

alloc參數(shù)allocWithZone 為 true

1.alloc---->allocWithZone--->true
2.初始化的方式,不但可以通過init還可以通過initXXX

new參數(shù)allocWithZone 為 false
  1. new---->allocWithZone--->false
  2. 初始化的方式,固定調(diào)用init
// Call [cls alloc] or [cls allocWithZone:nil], with appropriate 
// shortcutting optimizations.
static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
    if (slowpath(checkNil && !cls)) return nil;

#if __OBJC2__
    if (fastpath(!cls->ISA()->hasCustomAWZ())) {
        // No alloc/allocWithZone implementation. Go straight to the allocator.
        // fixme store hasCustomAWZ in the non-meta class and 
        // add it to canAllocFast's summary
        if (fastpath(cls->canAllocFast())) {
            // No ctors, raw isa, etc. Go straight to the metal.
            bool dtor = cls->hasCxxDtor();
            id obj = (id)calloc(1, cls->bits.fastInstanceSize());
            if (slowpath(!obj)) return callBadAllocHandler(cls);
            obj->initInstanceIsa(cls, dtor);
            return obj;
        }
        else {
            // Has ctor or raw isa or something. Use the slower path.
            id obj = class_createInstance(cls, 0);
            if (slowpath(!obj)) return callBadAllocHandler(cls);
            return obj;
        }
    }
#endif

    // No shortcuts available.
    if (allocWithZone) return [cls allocWithZone:nil];
    return [cls alloc];
}
是否重寫allocWithZone:方法
  • 沒有重寫allocWithZone:方法
    [cls new]分配內(nèi)存的方式和[cls alloc]分配方式是一樣的, 都是通過id obj = class_createInstance(cls, 0);來分配
  • 重寫了allocWithZone:方法
    [cls new]創(chuàng)建對象的方式[cls alloc]方法,
    [cls alloc]創(chuàng)建對象的方式[cls allocWithZone:nil]方法
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在日常開發(fā)中,有的人會(huì)用[[Class alloc] init]創(chuàng)建實(shí)例,也有的人會(huì)用[Class new]的方式...
    kwdx閱讀 3,678評論 7 12
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,667評論 1 32
  • 前言:這個(gè)知識(shí)點(diǎn)大多都已經(jīng)知曉,[[xx alloc] init] 跟 [xx new]是等價(jià)的。但是具體是如何等...
    好雨知時(shí)節(jié)浩宇閱讀 1,522評論 0 4
  • 前言 在面向?qū)ο缶幊讨校覀兠刻於荚趧?chuàng)建對象,用對象描述著整個(gè)世界,然而對象是如何從孕育到銷毀的呢? 目錄 1.孕...
    一縷殤流化隱半邊冰霜閱讀 12,232評論 75 161
  • Objective-C語言是一門動(dòng)態(tài)語言,它將很多靜態(tài)語言在編譯和鏈接時(shí)期做的事放到了運(yùn)行時(shí)來處理。這種動(dòng)態(tài)語言的...
    有一種再見叫青春閱讀 677評論 0 3

友情鏈接更多精彩內(nèi)容