5. self = [super init]
- (id)init
{
self = [super init];
if (self) {
// ETPCat does it own initialization here
}
return self;
}
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
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
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. 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.
28. 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;
29. Objective C Runtime
Dynamic binding
Dynamic binding is the process of mapping a message to a
method at runtime, rather than at compile time
32. 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
33. Use cases
Method swizzle (IIViewDeckController)
JSON Model (Torin ‘s BaseModel)
Message forwarding
Meta programming
35. 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));
36. 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
}
37. Use cases
JSON Model (Torin ‘s BaseModel)
updateWithDictionary
class_copyIvarList
ivar_getName