Objective-C Runtime

Objective-C is a language that defers a lot (almost anything possible) to runtime. Runtime is like an operating system for Objective-C. Deeper understanding about Runtime (how the messages are forwarded, how the classes are loaded dynamically…) will help you understand the better Objective-C programming and iOS.

Firstly, the runtime is Open-Source (http://opensource.apple.com/). The library is /usr/lib/libobjc.A.dylib

The Objective-C Runtime is a Runtime Library (written mainly in C & Assembler that adds the Object Oriented capabilities to C to create Objective-C). The Runtime Library loads in Class information, does method dispatching, forwarding, etc. essentially creates all the OOP support structures for Objective-C.

(There are 2 versions of runtime: Legacy runtime (all 32-bit Mac OS Apps) and Modern runtime (all 64-bit Mac OSX apps & all iOS apps) which was introduced with Objective-C 2.0. Modern runtime does not force you to recompile sub-classes if the super class’s layout has changed.

Secondly, everyone knows that [target doMethodWith:var1]; gets translated into objc_msgSend(target,@selector(doMethodWith:),var1); by the compiler. But to know more what happens, the process that objc_msgSend uses , we should read this blog post.

Interaction with Runtime

  1. Through Objective-C code:the message expressions are converted into objc_msgSend function calls (to learn about the conversion process, read this Guide).
  2. Through methods in NSObject: class, -isKindOfClass: , -isMemberOfClass: -respondsToSelector: , -conformsToProtocol:  
  3. Through direct calls to runtime functions: List is here

Swift and Runtime

The Runtime is written in Objective-C, and so to look at the 2 runtime techniques for Swift: associated_objects and method_swizzling ..


Objective-C Runtime Reference

List of methods

Important functions

  1. class_getClassMethod get a pointer to the data structure describing a given class method (for a given class). 
    Method     class_getClassMethod ( Class cls, SEL name );
  2. class_addMethod Adds a new method to a class (with a given name and implementation).
    BOOL        class_addMethod ( Class cls, SEL name, IMP imp, const char *types );
  3. class_replaceMethod

    Example (MagicalRecord) 

    void replaceSelectorForTargetWithSourceImpAndSwizzle(Class sourceClass, SEL sourceSelector, Class targetClass, SEL targetSelector)
    {
        Method sourceClassMethod = class_getClassMethod(sourceClass, sourceSelector);
        Method targetClassMethod = class_getClassMethod(targetClass, targetSelector);
        Class targetMetaClass = objc_getMetaClass([NSStringFromClass(targetClass) cStringUsingEncoding:NSUTF8StringEncoding]);
        BOOL methodWasAdded = class_addMethod(targetMetaClass, sourceSelector, 
    method_getImplementation(targetClassMethod), method_getTypeEncoding(targetClassMethod));
        if (methodWasAdded)
        { 
           class_replaceMethod(targetMetaClass, targetSelector,
                    method_getImplementation(sourceClassMethod),          
                    method_getTypeEncoding(sourceClassMethod));
        }
    }
Advertisements