DevPrep

All topics › Objective-C

What is the isa pointer?

Senior PRO

Essence

isa is the first field of every Objective-C object: a reference to its class. Through it objc_msgSend finds the method table and cache, and the runtime finds the lists of ivars, properties and protocols. A class also has an isa — it points to the metaclass, which holds the class methods (+). Metaclasses close the loop on the metaclass of the root class NSObject.

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

static void inspect(void) {
    NSObject *obj = [NSObject new];
    Class cls = object_getClass(obj);      // NSObject
    Class meta = object_getClass(cls);     // metaclass of NSObject
    BOOL isMeta = class_isMetaClass(meta); // YES
    NSLog(@"%@ %@ %d", cls, meta, isMeta);
}

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store