What root classes exist in iOS, and why are they needed?
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:
NSObject— the root of almost everything. The class implements theNSObjectprotocol (NSObjectProtocolin Swift), which describes that basic contract.NSProxy— an abstract "stand-in object" that implements only the minimum of theNSObjectprotocol and has noinit. Any message it lacks lands inmethodSignatureForSelector:+forwardInvocation:— it exists to forward messages to a real object: lazy loading, distributed objects (NSDistantObject), mocks, logging.
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