pavel kravchenko obj c runtime

24
Objective-C Runtime Pavel Kravchenko, Ciklum

Upload: dneprciklumevents

Post on 13-Dec-2014

823 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Pavel kravchenko obj c runtime

Objective-C Runtime

Pavel Kravchenko, Ciklum

Page 2: Pavel kravchenko obj c runtime

Agenda

What is runtime?

How we can use runtime?

How does it work?

What is object, class, isa, metaclass?

What can we do with runtime?

Page 3: Pavel kravchenko obj c runtime

What is it?

The runtime system acts as a kind of operating system for the Objective-C language

Page 4: Pavel kravchenko obj c runtime

Purpose

- understanding basic principles

- some useful things (class method, respondsToSelector, etc) → more simple, effective & understandable code

- tricks (dynamic loading, change of method implementation, simulation of multiple inheritance)

Page 5: Pavel kravchenko obj c runtime

Interacting with Runtime

through Objective-C source code

through NSObject methods

and through direct calls to runtime

Page 6: Pavel kravchenko obj c runtime

NSObject methods

class

IsKindOfClass:

IsMemberOfClass:

RespondsToSelector:

ConformsToProtocol:

MethodForSelector:

Page 7: Pavel kravchenko obj c runtime

Runtime methods

class_addMethod

class_replaceMethod

objc_setAssociatedObject

objc_getAssociatedObject

method_setImplementation

method_exchangeImplementations

Page 8: Pavel kravchenko obj c runtime

Basic Types typedef struct objc_selector *SEL;

typedef struct objc_class *Class;

typedef struct objc_object {

Class isa;

} *id

struct objc_class {

Class isa;

Class super_class;

}

struct objc_method {

SEL method_name;

char *method_types;

IMP method_imp;

}

Page 9: Pavel kravchenko obj c runtime

Classes and metaclasses

Page 10: Pavel kravchenko obj c runtime

Messaging

Page 11: Pavel kravchenko obj c runtime

Messaging

[target method:arg1 :arg2] →

objc_msgSend(target, selector, arg1, arg2)

SEL selector = @selector(method::);

SEL is unique identifier of the method name

Page 12: Pavel kravchenko obj c runtime

Messaging

■ It first finds the method implementation that the selector refers to.

■ It then calls the procedure, passing it the receiving object, along with any arguments that were specified for the method.

■ Finally, it passes on the return value of the procedure as its own return value.

Page 13: Pavel kravchenko obj c runtime

Dynamic Method Resolution

void dynamicMethodIMP(id self, SEL _cmd) {

// implementation ....

}

+ (BOOL) resolveInstanceMethod:(SEL) aSEL {

if (aSEL == @selector(resolveThisMethodDynamically)) {

class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");

return YES;

}

return [super resolveInstanceMethod:aSEL];

}

Page 14: Pavel kravchenko obj c runtime

Message Forwarding

2011-03-26 07:32:26.268 First[48403:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance 0x4e0cc70'

*** Call stack at first throw:

(

0 CoreFoundation 0x00db4be9 __exceptionPreprocess + 185

1 libobjc.A.dylib 0x00f095c2 objc_exception_throw + 47

2 CoreFoundation 0x00db66fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187

3 CoreFoundation 0x00d26366 ___forwarding___ + 966

4 CoreFoundation 0x00d25f22 _CF_forwarding_prep_0 + 50

5 First 0x00002616 -[My1 newM] + 101

6 First 0x00001f00 -[My1 mySelector] + 50

Page 15: Pavel kravchenko obj c runtime

Message Forwarding

Sending a message to object that does not handle it is an error

Before error (crash) runtime gives the receiving object second chance to handle wrong message

We need to implement methods of another class, but cannot subclass it

Page 16: Pavel kravchenko obj c runtime

Message Forwarding

Page 17: Pavel kravchenko obj c runtime

Message Forwarding

- (void)forwardInvocation:(NSInvocation *)

anInvocation {

if ([someOtherObject respondsToSelector:

[anInvocation selector]])

[anInvocation

invokeWithTarget:someOtherObject];

else

[super forwardInvocation:anInvocation];

}

Page 18: Pavel kravchenko obj c runtime

Message Forwarding

@implementation NSObject (NoCrash)

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

{

NSMethodSignature *methodSignature = [NSMethodSignature

signatureWithObjCTypes:"v@:"];

return methodSignature;

}

- (void) forwardInvocation:(NSInvocation *)anInvocation

{

}

Page 19: Pavel kravchenko obj c runtime

Tricks

id classObj = objc_getClass("SomeClass");

unsigned int outCount, i;

objc_property_t *properties = class_copyPropertyList(classObj,

&outCount);

for (i = 0; i < outCount; i++) {

objc_property_t property = properties[i];

NSString *selector = [NSString

stringWithCString:property_getName(property)];

SEL selForProperty = NSSelectorFromString(selector);

NSLog(@"%@", [self performSelector:selForProperty]);

}

Page 20: Pavel kravchenko obj c runtime

KVO

Automatic key-value observing is implemented using a technique called isa-swizzling.

What KVO does when you request to observe some property:

1. Gets the class of the object

2. Creates a new subclass of the object's class

3. Overrides the -class method with one that returns the Class of the original class

4. Overrides the desired setter methods in the original class

5. Sets the isa variable of the object to the new subclass

Page 21: Pavel kravchenko obj c runtime

Method swizzling

DataObject * dataObject = [[DataObject alloc] init];

[dataObject addObserver:self forKeyPath:@"dataValue" options:0 context:NULL];

NSLog(@"%@", [dataObject class]); //logs "DataObject"

NSLog(@"%@", object_getClass(dataObject)); //logs "NSKVONotifying_DataObject"

Page 22: Pavel kravchenko obj c runtime

Method swizzling

Method existingMethod = class_getInstanceMethod([self class],

@selector(viewDidLoad));

Method myNewMethod = class_getInstanceMethod([self class],

@selector(viewDidLoadMy));

method_exchangeImplementations(existingMethod, myNewMethod);

@implementation UIViewController (someCategory)

- (void) viewDidLoadMy

{

[self viewDidLoadMy];

NSLog(@"Print info");

}

Page 23: Pavel kravchenko obj c runtime

Links

http://touchdev.ru/documents/958

http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html

http://cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html

http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html

Objective C Runtime Programming Guide

Objective C Runtime Reference

Page 24: Pavel kravchenko obj c runtime

About me

Pavel Kravchenko

skype: ideateam_macuser

email:

[email protected]

Interests:

iOS, Android, security

Manadgment