Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C Runtime

林藍東
March 05, 2012

Objective-C Runtime

珠三角技术沙龙上关于Objective-C运行时的一个分享

林藍東

March 05, 2012
Tweet

Other Decks in Programming

Transcript

  1. History Brad 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
  2. @interface NSObject <NSObject> { Class isa; } What is an

    Object ? struct NSObject { Class isa; }
  3. What is an Object ? @interface MyName : NSObject {

    NSString *firstName; NSString *lastName; } struct MyName { NSString *firstName; NSString *lastName; } Class isa;
  4. What is an Object ? NSObject isa 0 MyName isa

    firstName lastName 0 4 8 NSObject isa 0 4 foobar MyName isa firstName lastName 0 4 8 MyName isa firstName lastName 0 4 8 12 foobar
  5. 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
  6. isa-swizzling MyName (instance) MyName (Class) isa HerName (Class) isa MyName

    *myName = [MyName alloc] init]; myName->isa = [HerName class];
  7. 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.
  8. Messaging Smalltalk 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
  9. Messaging Selector typedef struct objc_selector *SEL; IMP typedef id (*IMP)(id,

    SEL, ...); id typedef struct objc_object { Class isa; } *id;
  10. Messaging @implementation MyName - (void)printName:(NSString *)name { ...... } @end

    void - [MyName printName:](id self, SEL _cmd, NSString *name) { ...... }
  11. 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;