Write first: Record a recently seen question, is the instance object created by alloc or init? Write not good, also please see the person light spray!

I have a Person class that declares an age property

    Person *p = [Person alloc];
    p.age = 10;
    Person *p1 = [p init];
    Person *p2 = [p init];
Copy the code

First two questions: Q1: Are P1 and P2 the same? Q2: What are the values of p1.age and p2.age? (0 or 10) Let’s look at the result Q1:

p1=<Person: 0x6000029bca80>
p2=<Person: 0x6000029bca80>
Copy the code

By its memory address can see the two objects are the same (but no memory address is enough to prove that the two objects are the same thing, not necessarily, p1, p2) at the time of initialization, initialization of the content may be different)

The results of Q2:

p1.age=10
p2.age=10
Copy the code

From this result, it seems that:

Person *p1 = [p init]; Person *p2 = [p init]; Equivalent to Person *p1 = p; Person *p2 = p; Oh, init doesn't seem to work, does it?Copy the code

So let’s see what the alloc method does with objC’s NSObject code:

1. + (id)alloc { reeturn _objc_rootAlloc(self); _objc_rootAlloc id _objc_rootAlloc(Class CLS) {return callAlloc(cls, false/*chechNil*/, true/*allocWithZone*/); CallAlloc static id callAlloc(Class CLS, bool checkNil, bool allocWithZone=false)
{
    if(checkNil && ! cls)return nil;
    
    if(! cls->ISA()->hasCustomAWZ()) { // No alloc/allocWithZone implementation. Go straight to the allocator. // fixme store hasCustomAWZin 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; } } // No shortcuts available. if (allocWithZone) return [cls allocWithZone:nil]; return [cls alloc]; } obj = class_createInstance(CLS, 0); Id class_createInstance(Class CLS, size_t extraBytes) {return _class_createInstanceFromZone(CLS, extraBytes, nil); } _class_createInstanceFromZone static id _class_createInstanceFromZone(Class CLS, size_t extraBytes, void *zone, bool cxxConstruct = true, size_t *outAllocatedSize = nil) { if (! cls) return nil; // Determine whether to assert(CLS ->isRealized()); // Read class's info bits all at once forperformance bool hasCxxCtor = cls->hasCxxCtor(); bool hasCxxDtor = cls->hasCxxDtor(); bool fast = cls->canAllocNonpointer(); size_t size = cls->instanceSize(extraBytes); // This step is to get the size of the objectif (outAllocatedSize) *outAllocatedSize = size;
    id obj;
    if(! zone && fast) { obj = (id)calloc(1, size); // Allocate spaceif(! obj)returnnil; obj->initInstanceIsa(cls, hasCxxDtor); // initialize}else {
        if (zone) {
            obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size);
        } else {
            obj = (id)calloc(1, size);
        }
        if(! obj)return nil;

        // Use raw pointer isa on the assumption that they might be 
        // doing something weird with the zone or RR.
        obj->initIsa(cls);
    }
    if (cxxConstruct && hasCxxCtor) {
        obj = _objc_constructOrFree(obj, cls);
    }

    returnobj; } obj = (id)calloc(1, size);if(! obj)return nil;
obj->initInstanceIsa(cls, hasCxxDtor);

Copy the code

Person *p = [Person alloc]; When running to the breakpoint, go to the Xcode menu bar –>Debug–>DebugWorkflow–>Always Show Disassembly.