0
Objective C Runtime
Khoa Pham
2359Media
Today
● self = [super init]
● objc_msgSend
● ObjC Runtime
● How to use the Runtime API
● Use cases
self = [super init]
ETPAnimal *cat = [ETPAnimal cat];
NSInteger recordCount = [ETPCoreDataManager
recordCount];
self = [super init]
@interface ETPCat : ETPAnimal
@end
ETPCat *cat = [[ETPCat alloc] init]
self = [super init]
- (id)init
{
self = [super init];
if (self) {
// ETPCat does it own initialization here
}
return self;...
self = [super init]
[super init] calls the superclass implementation of init with
the (hidden) self argument.
It can do on...
self = [super init]
ETPCat *cat = [ETPCat alloc] // 0x1111111a
[cat init] // 0x1111111b
[cat meomeo] // 0x1111111a
self = [super init]
Demo
objc_msgSend
ETPCat *cat = [[ETPCat alloc] init]
[cat setName:@”meo”]
objc_msgSend(cat, @selector(setName:), @”meo”)
objc_msgSend
Demo
objc_msgSend
@selector
SEL selector1 = @selector(initWithName:)
SEL selector2 = @selector(initWithFriends1Name::)
typedef struct objc_s...
@selector
Demo
Objective C Runtime
The Objective-C Runtime is a Runtime Library, it's a library
written mainly in C & Assembler that adds...
Objective C Runtime
Source code http://www.opensource.apple.
com/source/objc4/objc4-532/runtime/objc-class.mm
There are tw...
Objective C Runtime
Dynamic feature
Object oriented capability
Objective C Runtime
Features
● Class elements (categories, methods, variables,
property, …)
● Object
● Messaging
● Object ...
Objective C Runtime
@interface ETPAnimal : NSObject
@end
typedef struct objc_class *Class;
Objective C Runtime (old)
struct objc_class {
Class isa;
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVA...
Objective C Runtime
typedef struct class_ro_t
{
const char * name;
const ivar_list_t * ivars;
} class_ro_t
typedef struct ...
Objective C Runtime
ETPAnimal *animal = [[ETPAnimal alloc] init]
struct objc_object
{
Class isa;
// variables
};
Objective C Runtime
id someAnimal = [[ETPAnimal alloc] init]
typedef struct objc_object
{
Class isa;
} *id;
Objective C Runtime
Class is also an object, its isa pointer points to its meta
class
The metaclass is the description of ...
Objective C Runtime
Objective C Runtime
Demo
Objective C Runtime
● Dynamic typing
● Dynamic binding
● Dynamic method resolution
● Introspection
Objective C Runtime
Dynamic typing
Dynamic typing enables the runtime to determine the type
of an object at runtime
id cat...
Objective C Runtime
Dynamic binding
Dynamic binding is the process of mapping a message to a
method at runtime, rather tha...
Objective C Runtime
Dynamic method resolution
Provide the implementation of a method dynamically.
@dynamic
Objective C Runtime
Introspection
isKindOfClass
respondsToSelector
conformsToProtocol
How to use the Runtime API
Objective-C programs interact with the runtime system to
implement the dynamic features of the ...
Use cases
Method swizzle (IIViewDeckController)
JSON Model (Torin ‘s BaseModel)
Message forwarding
Meta programming
Use cases
Method swizzle
Use cases
Method swizzle (IIViewDeckController)
SEL presentVC = @selector(presentViewController:animated:completion:);
SEL...
Use cases
Method swizzle (IIViewDeckController)
- (void)vdc_presentViewController:(UIViewController *)viewControllerToPres...
Use cases
JSON Model (Torin ‘s BaseModel)
updateWithDictionary
class_copyIvarList
ivar_getName
Use cases
JSON Model (Torin ‘s BaseModel)
@interface ETPItem : BaseModel
@property (nonatomic, copy) NSString * ID;
@prope...
Message forwarding
Use cases
Meta programming
● Dynamic method naming
● Validation
● Template
● Mocking
Reference
1. http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html
2. http://www.slideshare.net/...
Thank you
Q&A
Objective-C Runtime overview
Upcoming SlideShare
Loading in...5
×

Objective-C Runtime overview

228

Published on

Introduction to Objective-C Runtime and its benefits

Published in: Software, Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
228
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Objective-C Runtime overview"

  1. 1. Objective C Runtime Khoa Pham 2359Media
  2. 2. Today ● self = [super init] ● objc_msgSend ● ObjC Runtime ● How to use the Runtime API ● Use cases
  3. 3. self = [super init] ETPAnimal *cat = [ETPAnimal cat]; NSInteger recordCount = [ETPCoreDataManager recordCount];
  4. 4. self = [super init] @interface ETPCat : ETPAnimal @end ETPCat *cat = [[ETPCat alloc] init]
  5. 5. self = [super init] - (id)init { self = [super init]; if (self) { // ETPCat does it own initialization here } return self; }
  6. 6. 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
  7. 7. self = [super init] ETPCat *cat = [ETPCat alloc] // 0x1111111a [cat init] // 0x1111111b [cat meomeo] // 0x1111111a
  8. 8. self = [super init] Demo
  9. 9. objc_msgSend ETPCat *cat = [[ETPCat alloc] init] [cat setName:@”meo”] objc_msgSend(cat, @selector(setName:), @”meo”)
  10. 10. objc_msgSend Demo
  11. 11. objc_msgSend
  12. 12. @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
  13. 13. @selector Demo
  14. 14. 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.
  15. 15. 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.
  16. 16. Objective C Runtime Dynamic feature Object oriented capability
  17. 17. Objective C Runtime Features ● Class elements (categories, methods, variables, property, …) ● Object ● Messaging ● Object introspection
  18. 18. Objective C Runtime @interface ETPAnimal : NSObject @end typedef struct objc_class *Class;
  19. 19. 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; }
  20. 20. 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;
  21. 21. Objective C Runtime ETPAnimal *animal = [[ETPAnimal alloc] init] struct objc_object { Class isa; // variables };
  22. 22. Objective C Runtime id someAnimal = [[ETPAnimal alloc] init] typedef struct objc_object { Class isa; } *id;
  23. 23. 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
  24. 24. Objective C Runtime
  25. 25. Objective C Runtime Demo
  26. 26. Objective C Runtime ● Dynamic typing ● Dynamic binding ● Dynamic method resolution ● Introspection
  27. 27. 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;
  28. 28. Objective C Runtime Dynamic binding Dynamic binding is the process of mapping a message to a method at runtime, rather than at compile time
  29. 29. Objective C Runtime Dynamic method resolution Provide the implementation of a method dynamically. @dynamic
  30. 30. Objective C Runtime Introspection isKindOfClass respondsToSelector conformsToProtocol
  31. 31. 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
  32. 32. Use cases Method swizzle (IIViewDeckController) JSON Model (Torin ‘s BaseModel) Message forwarding Meta programming
  33. 33. Use cases Method swizzle
  34. 34. 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));
  35. 35. 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 }
  36. 36. Use cases JSON Model (Torin ‘s BaseModel) updateWithDictionary class_copyIvarList ivar_getName
  37. 37. 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”}];
  38. 38. Message forwarding
  39. 39. Use cases Meta programming ● Dynamic method naming ● Validation ● Template ● Mocking
  40. 40. 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
  41. 41. Thank you Q&A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×