DevPrep

All topics › Objective-C

What root classes exist in iOS, and why are they needed?

Middle PRO

Essence

A root class is a class without a superclass, the top of a hierarchy. It gives all descendants the basic contract with the runtime: isa, alloc/init/dealloc, reference counting, introspection (class, isKindOfClass:, respondsToSelector:), isEqual:/hash, description, forwarding. Foundation has two root classes:

Technically you can declare your own root class (__attribute__((objc_root_class)) or the NS_ROOT_CLASS macro), but then you write alloc, retain/release etc. yourself — nobody does this in practice.

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