iOS 中 alloc init 和new的區(qū)別

前言:這個(gè)知識(shí)點(diǎn)大多都已經(jīng)知曉,[[xx alloc] init] 跟 [xx new]是等價(jià)的。但是具體是如何等價(jià)的或許大多數(shù)人都解釋不清楚,知識(shí)單純的知道結(jié)論。這篇博文從源碼角度來解釋下為什么說二者是等價(jià)的。

一 、分別看下alloc init 源碼實(shí)現(xiàn):

1、alloc源碼實(shí)現(xiàn)如下:

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

內(nèi)部調(diào)用了 _objc_rootAlloc()函數(shù),繼續(xù)看下_objc_rootAlloc實(shí)現(xiàn)源碼如下:

id _objc_rootAlloc(Class cls) {
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

可以看到內(nèi)部又調(diào)用了:callAlloc函數(shù)。繼續(xù)看callAlloc的實(shí)現(xiàn)如下:(這里我們留意一下調(diào)用callAlloc函數(shù)時(shí)傳的參數(shù)值!

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];
}

我們簡單看一下該函數(shù)的實(shí)現(xiàn):

1、上面做了對象判空操作,之后做了預(yù)編譯的處理,并根據(jù)cls->ISA()->hasCustomAWZ()判斷是否實(shí)現(xiàn)allocWithZone函數(shù),如果類自己實(shí)現(xiàn)了則不會(huì)執(zhí)行if下面的邏輯。(!!!:重寫allocWithZone的情況下改代碼不會(huì)執(zhí)行。)
2、再根據(jù)canAllocFast判斷是否支持快速創(chuàng)建。

3、執(zhí)行完上面預(yù)編譯宏內(nèi)的邏輯后,來到關(guān)鍵的地方,也就是

// No shortcuts available.
    if (allocWithZone) return [cls allocWithZone:nil];
    return [cls alloc];

這里我們可以清晰的看到,如果allocWithZone為true,則會(huì)執(zhí)行allocWithZon函數(shù),反之 直接執(zhí)行alloc函數(shù)。

我們得出作出第一個(gè)總結(jié):
結(jié)論1:在重寫了allocWithZone的前提下,調(diào)用alloc函數(shù)時(shí),在callAlloc函數(shù)內(nèi)部最終會(huì)執(zhí)行[cls allocWithZone:nil];

二、上面是alloc的源碼實(shí)現(xiàn),我們再看下new的源碼實(shí)現(xiàn):

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}

我們可以看到new函數(shù)中直接調(diào)用了callAlloc函數(shù)(也就是上面我們分析的函數(shù)),而且它調(diào)用callAlloc函數(shù)時(shí)傳遞的參數(shù)中,沒有傳遞allocWithZone的值,根據(jù)上面的函數(shù)實(shí)現(xiàn),如果不傳值會(huì)取默認(rèn)值,即:allocWithZone = false。所以:
結(jié)論2:在重寫了allocWithZone的前提下,調(diào)用new函數(shù),在callAlloc函數(shù)內(nèi)部最終會(huì)執(zhí)行[cls alloc],然后再走一遍上面分析的alloc的邏輯,最終執(zhí)行 [cls allocWithZone:nil]

三、結(jié)論

經(jīng)過上面的對比分析,我們可以得出結(jié)論:

在重寫了allocWithZone函數(shù)后,alloc init 和 new的區(qū)別只是在于:是否顯示的調(diào)用allocWithZone函數(shù)。使用new時(shí)會(huì)在callAlloc中調(diào)用alloc,從而隱式調(diào)用allocWithZone:函數(shù)。

注:如果沒有重寫allocWithZone:函數(shù),使用alloc init同new沒有任何區(qū)別。

最后編輯于
?著作權(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ù)。

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