Slide 1

Slide 1 text

Objective C Runtime Khoa Pham 2359Media

Slide 2

Slide 2 text

Today ● self = [super init] ● objc_msgSend ● ObjC Runtime ● How to use the Runtime API ● Use cases

Slide 3

Slide 3 text

self = [super init] ETPAnimal *cat = [ETPAnimal cat]; NSInteger recordCount = [ETPCoreDataManager recordCount];

Slide 4

Slide 4 text

self = [super init] @interface ETPCat : ETPAnimal @end ETPCat *cat = [[ETPCat alloc] init]

Slide 5

Slide 5 text

self = [super init] - (id)init { self = [super init]; if (self) { // ETPCat does it own initialization here } return self; }

Slide 6

Slide 6 text

self = [super init] [super init] calls the superclass implementation of init with the (hidden) self argument. It can do one of these things + set some properties on self, and return self + return a different object (factory, singleton) + return nil

Slide 7

Slide 7 text

self = [super init] ETPCat *cat = [ETPCat alloc] // 0x1111111a [cat init] // 0x1111111b [cat meomeo] // 0x1111111a

Slide 8

Slide 8 text

self = [super init] Demo

Slide 9

Slide 9 text

objc_msgSend ETPCat *cat = [[ETPCat alloc] init] [cat setName:@”meo”] objc_msgSend(cat, @selector(setName:), @”meo”)

Slide 10

Slide 10 text

objc_msgSend Demo

Slide 11

Slide 11 text

objc_msgSend

Slide 12

Slide 12 text

@selector SEL selector1 = @selector(initWithName:) SEL selector2 = @selector(initWithFriends1Name::) typedef struct objc_selector *SEL Read more at Objective C Runtime Reference -> Data Structure -> Class definition Data structure -> SEL

Slide 13

Slide 13 text

@selector Demo

Slide 14

Slide 14 text

Objective C Runtime The Objective-C Runtime is a Runtime Library, it's a library written mainly in C & Assembler that adds the Object Oriented capabilities to C to create Objective-C.

Slide 15

Slide 15 text

Objective C Runtime Source code http://www.opensource.apple. com/source/objc4/objc4-532/runtime/objc-class.mm There are two versions of the Objective-C runtime— “modern” and “legacy”. The modern version was introduced with Objective-C 2.0 and includes a number of new features.

Slide 16

Slide 16 text

Objective C Runtime Dynamic feature Object oriented capability

Slide 17

Slide 17 text

Objective C Runtime Features ● Class elements (categories, methods, variables, property, …) ● Object ● Messaging ● Object introspection

Slide 18

Slide 18 text

Objective C Runtime @interface ETPAnimal : NSObject @end typedef struct objc_class *Class;

Slide 19

Slide 19 text

Objective C Runtime (old) struct objc_class { Class isa; Class super_class OBJC2_UNAVAILABLE; const char *name OBJC2_UNAVAILABLE; long version OBJC2_UNAVAILABLE; long info OBJC2_UNAVAILABLE; long instance_size OBJC2_UNAVAILABLE; struct objc_ivar_list *ivars OBJC2_UNAVAILABLE; struct objc_method_list **methodLists OBJC2_UNAVAILABLE; struct objc_cache *cache OBJC2_UNAVAILABLE; struct objc_protocol_list *protocols OBJC2_UNAVAILABLE; }

Slide 20

Slide 20 text

Objective C Runtime typedef struct class_ro_t { const char * name; const ivar_list_t * ivars; } class_ro_t typedef struct class_rw_t { const class_ro_t *ro; method_list_t **methods; struct class_t *firstSubclass; struct class_t *nextSiblingClass; } class_rw_t;

Slide 21

Slide 21 text

Objective C Runtime ETPAnimal *animal = [[ETPAnimal alloc] init] struct objc_object { Class isa; // variables };

Slide 22

Slide 22 text

Objective C Runtime id someAnimal = [[ETPAnimal alloc] init] typedef struct objc_object { Class isa; } *id;

Slide 23

Slide 23 text

Objective C Runtime Class is also an object, its isa pointer points to its meta class The metaclass is the description of the class object

Slide 24

Slide 24 text

Objective C Runtime

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Objective C Runtime Demo

Slide 27

Slide 27 text

Objective C Runtime ● Dynamic typing ● Dynamic binding ● Dynamic method resolution ● Introspection

Slide 28

Slide 28 text

Objective C Runtime Dynamic typing Dynamic typing enables the runtime to determine the type of an object at runtime id cat = [[ETPCat alloc] init] - (void)acceptAnything:(id)anything;

Slide 29

Slide 29 text

Objective C Runtime Dynamic binding Dynamic binding is the process of mapping a message to a method at runtime, rather than at compile time

Slide 30

Slide 30 text

Objective C Runtime Dynamic method resolution Provide the implementation of a method dynamically. @dynamic

Slide 31

Slide 31 text

Objective C Runtime Introspection isKindOfClass respondsToSelector conformsToProtocol

Slide 32

Slide 32 text

How to use the Runtime API Objective-C programs interact with the runtime system to implement the dynamic features of the language. ● Objective-C source code ● Foundation Framework NSObject methods ● Runtime library API

Slide 33

Slide 33 text

Use cases Method swizzle (IIViewDeckController) JSON Model (Torin ‘s BaseModel) Message forwarding Meta programming

Slide 34

Slide 34 text

Use cases Method swizzle

Slide 35

Slide 35 text

Use cases Method swizzle (IIViewDeckController) SEL presentVC = @selector(presentViewController:animated:completion:); SEL vdcPresentVC = @selector(vdc_presentViewController:animated:completion:); method_exchangeImplementations(class_getInstanceMethod(self, presentVC), class_getInstanceMethod(self, vdcPresentVC));

Slide 36

Slide 36 text

Use cases Method swizzle (IIViewDeckController) - (void)vdc_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL) animated completion:(void (^)(void))completion { UIViewController* controller = self.viewDeckController ?: self; [controller vdc_presentViewController:viewControllerToPresent animated:animated completion: completion]; // when we get here, the vdc_ method is actually the old, real method }

Slide 37

Slide 37 text

Use cases JSON Model (Torin ‘s BaseModel) updateWithDictionary class_copyIvarList ivar_getName

Slide 38

Slide 38 text

Use cases JSON Model (Torin ‘s BaseModel) @interface ETPItem : BaseModel @property (nonatomic, copy) NSString * ID; @property (nonatomic, copy) NSString *name; @end ETPItem *item = [[ETPItem alloc] init]; [item updateWithDictionary:@{@”ID”: @”1”, @”name”: @”item1”}];

Slide 39

Slide 39 text

Message forwarding

Slide 40

Slide 40 text

Use cases Meta programming ● Dynamic method naming ● Validation ● Template ● Mocking

Slide 41

Slide 41 text

Reference 1. http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html 2. http://www.slideshare.net/mudphone/what-makes-objective-c-dynamic 3. http://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html 4. http://nshipster.com/method-swizzling/ 5. http://stackoverflow.com/questions/415452/object-orientation-in-c 6. http://stackoverflow.com/questions/2766233/what-is-the-c-runtime-library 7. http://gcc.gnu.org/onlinedocs/gcc/Modern-GNU-Objective-C-runtime-API.html 8. http://www.opensource.apple.com/source/objc4/objc4-532/runtime/objc-class.mm 9. http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/ 10. https://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html 11. Pro Objective C, chapter 7, 8, 9 12. Effective Objective C, chapter 2 13. http://wiki.gnustep.org/index.php/ObjC2_FAQ

Slide 42

Slide 42 text

Thank you Q&A