19
Objective-C Runtime [email protected] Sunday, October 30, 2011

Objective runtime

Embed Size (px)

Citation preview

Page 1: Objective runtime

Objective-C Runtime

林 藍 東[email protected]

Sunday, October 30, 2011

Page 2: Objective runtime

HistoryBrad Cox, Tom Love, 1981, ITT Corp.

Reusability & Backward Compatibility with C

Smalltalk + C = Object-Oriented Pre-Compiler

Objective-C, StepStone

1988, NeXT, AppKit & Foundation Kit

Sunday, October 30, 2011

Page 3: Objective runtime

@interface NSObject <NSObject> { Class isa;

}

What is an Object ?

struct NSObject { Class isa;

}

Sunday, October 30, 2011

Page 4: Objective runtime

What is an Object ?@interface MyName : NSObject {

NSString *firstName;NSString *lastName;

}

struct MyName {

NSString *firstName;NSString *lastName;

}

Class isa;

Sunday, October 30, 2011

Page 5: Objective runtime

What is an Object ?NSObject

isa0

MyNameisa

firstNamelastName

048

NSObjectisa0

4 foobar

MyNameisa

firstNamelastName

048

MyNameisa

firstNamelastName

04812

foobar

Sunday, October 30, 2011

Page 6: Objective runtime

struct objc_class { Class isa;

#if !__OBJC2__ 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;#endif

} OBJC2_UNAVAILABLE;

typedef struct objc_class *Class;

/usr/include/objc/runtime.h

Sunday, October 30, 2011

Page 7: Objective runtime

MyName(instance)

MyName(Class)

MyName(MetaClass)

NSObject(Class)

NSObject(MetaClass)

isa

isa

super

super

super

isa

isasuper

isa

Object Model

Sunday, October 30, 2011

Page 8: Objective runtime

isa-swizzling

MyName(instance)

MyName(Class)

isa

HerName(Class)

isa

MyName *myName = [MyName alloc] init];

myName->isa = [HerName class];

Sunday, October 30, 2011

Page 9: Objective runtime

isa-swizzlingKey-Value Observing

State Machine

......

NSZombie

Sunday, October 30, 2011

Page 10: Objective runtime

Where the Objects Come From

The complexity is handled through each object performing its own functions, without undue interference from others.

The robustness comes from the fact that the loss of an object does not damage other objects.

Supporting growth comes from using the same structuring and communication mechanism throughout.

Finally, the reuse comes from each object performing its own role, with only minimal connections to other objects.

<<Squeak, Open Personal Computing and Multimedia>>

Object is a metaphor of cell.

Sunday, October 30, 2011

Page 11: Objective runtime

MessagingSmalltalk is not only NOT its syntax or the class library, it is not even about classes.

I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea.

The big idea is "messaging".

---- Alan Kay

Sunday, October 30, 2011

Page 12: Objective runtime

MessagingSelector

typedef struct objc_selector *SEL;

IMPtypedef id (*IMP)(id, SEL, ...);

id

typedef struct objc_object { Class isa;} *id;

Sunday, October 30, 2011

Page 13: Objective runtime

Messaging@implementation MyName

- (void)printName:(NSString *)name{

......}

@end

void - [MyName printName:](id self, SEL _cmd, NSString *name){

......}

Sunday, October 30, 2011

Page 14: Objective runtime

Messaging [name printName:@”lldong”];

objc_msgSend(name, @selector(printName:), @”lldong”);

Sunday, October 30, 2011

Page 15: Objective runtime

Messaging

id objc_msgSend(id receiver, SEL name, arguments...){ IMP function = Class_getMethodImplementation(receiver->isa, name); return function(arguments);}

IMP class_getMethodImplementation(Class cls, SEL name);

- (IMP) methodForSelector:(SEL)sel;

Sunday, October 30, 2011

Page 16: Objective runtime

MyName(instance)

MyName(Class)

MyName(MetaClass)

NSObject(Class)

NSObject(MetaClass)

isa

isa

super

super

super

isa

isasuper

isa

Sunday, October 30, 2011

Page 17: Objective runtime

Messaging

- (void)forwardInvocation:(NSInvocation *)invocation

+ (BOOL)resolveInstanceMethod:(SEL)sel+ (BOOL)resolveClassMethod:(SEL)sel

- (id)forwardingTargetForSelector:(SEL)sel

1

2

3

Sunday, October 30, 2011

Page 18: Objective runtime

Conclusion

NO MAGIC

Sunday, October 30, 2011

Page 19: Objective runtime

Q & A

allContents

Sunday, October 30, 2011