42
Objective C Runtime Khoa Pham 2359Media

Objective-C Runtime overview

Embed Size (px)

DESCRIPTION

Introduction to Objective-C Runtime and its benefits

Citation preview

Page 1: Objective-C Runtime overview

Objective C RuntimeKhoa Pham2359Media

Page 2: Objective-C Runtime overview

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

Page 3: Objective-C Runtime overview

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

Page 4: Objective-C Runtime overview

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

ETPCat *cat = [[ETPCat alloc] init]

Page 5: Objective-C Runtime overview

self = [super init]- (id)init{

self = [super init];if (self) {

// ETPCat does it own initialization here}return self;

}

Page 6: Objective-C Runtime overview

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

Page 7: Objective-C Runtime overview

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

Page 8: Objective-C Runtime overview

self = [super init]

Demo

Page 9: Objective-C Runtime overview

objc_msgSendETPCat *cat = [[ETPCat alloc] init][cat setName:@”meo”]

objc_msgSend(cat, @selector(setName:), @”meo”)

Page 10: Objective-C Runtime overview

objc_msgSend

Demo

Page 11: Objective-C Runtime overview

objc_msgSend

Page 12: Objective-C Runtime overview

@selectorSEL 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

Page 13: Objective-C Runtime overview

@selector

Demo

Page 14: Objective-C Runtime overview

Objective C RuntimeThe 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.

Page 15: Objective-C Runtime overview

Objective C RuntimeSource 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.

Page 16: Objective-C Runtime overview

Objective C RuntimeDynamic featureObject oriented capability

Page 17: Objective-C Runtime overview

Objective C RuntimeFeatures● Class elements (categories, methods, variables,

property, …)● Object● Messaging● Object introspection

Page 18: Objective-C Runtime overview

Objective C Runtime@interface ETPAnimal : NSObject@end

typedef struct objc_class *Class;

Page 19: Objective-C Runtime overview

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;

}

Page 20: Objective-C Runtime overview

Objective C Runtimetypedef 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;

Page 21: Objective-C Runtime overview

Objective C RuntimeETPAnimal *animal = [[ETPAnimal alloc] init]

struct objc_object{

Class isa;// variables

};

Page 22: Objective-C Runtime overview

Objective C Runtimeid someAnimal = [[ETPAnimal alloc] init]

typedef struct objc_object{ Class isa;} *id;

Page 23: Objective-C Runtime overview

Objective C RuntimeClass is also an object, its isa pointer points to its meta class

The metaclass is the description of the class object

Page 24: Objective-C Runtime overview

Objective C Runtime

Page 25: Objective-C Runtime overview
Page 26: Objective-C Runtime overview

Objective C Runtime

Demo

Page 27: Objective-C Runtime overview

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

Page 28: Objective-C Runtime overview

Objective C RuntimeDynamic typingDynamic typing enables the runtime to determine the type of an object at runtime

id cat = [[ETPCat alloc] init]- (void)acceptAnything:(id)anything;

Page 29: Objective-C Runtime overview

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

Page 30: Objective-C Runtime overview

Objective C RuntimeDynamic method resolutionProvide the implementation of a method dynamically.

@dynamic

Page 31: Objective-C Runtime overview

Objective C RuntimeIntrospection

isKindOfClassrespondsToSelectorconformsToProtocol

Page 32: Objective-C Runtime overview

How to use the Runtime APIObjective-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

Page 33: Objective-C Runtime overview

Use casesMethod swizzle (IIViewDeckController)JSON Model (Torin ‘s BaseModel)Message forwardingMeta programming

Page 34: Objective-C Runtime overview

Use casesMethod swizzle

Page 35: Objective-C Runtime overview

Use casesMethod 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));

Page 36: Objective-C Runtime overview

Use casesMethod 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

}

Page 37: Objective-C Runtime overview

Use casesJSON Model (Torin ‘s BaseModel)

updateWithDictionary

class_copyIvarList

ivar_getName

Page 38: Objective-C Runtime overview

Use casesJSON 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”}];

Page 39: Objective-C Runtime overview

Message forwarding

Page 40: Objective-C Runtime overview

Use casesMeta programming

● Dynamic method naming

● Validation

● Template

● Mocking

Page 41: Objective-C Runtime overview

Reference1. 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

Page 42: Objective-C Runtime overview

Thank youQ&A