self and super in objc

2,521 views
2,400 views

Published on

explain self and super in objc's class

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,521
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
25
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

self and super in objc

  1. 1. Obj-C self super twitter:@vonbo
  2. 2. self class is Derivedsuper class is Derived
  3. 3. obj-c• [self class] or [super class]• self• super “ ” self super class
  4. 4. C -1• Sending Messages• When it encounters a method invocation, the compiler might generate a call to any of several functions to perform the actual message dispatch, depending on the receiver, the return value, and the arguments.• You can use these functions to dynamically invoke methods from your own plain C code, or to use argument forms not permitted by NSObject’s perform… methods. These functions are declared in /usr/include/objc/objc-runtime.h.
  5. 5. C -2• objc_msgSend sends a message with a simple return value to an instance of a class.• objc_msgSend_stret sends a message with a data- structure return value to an instance of a class.• objc_msgSendSuper sends a message with a simple return value to the superclass of an instance of a class.• objc_msgSendSuper_stret sends a message with a data-structure return value to the superclass of an instance of a class.uperclass of an instance of a class.
  6. 6. objc_msgSend• • id objc_msgSend(id theReceiver, SEL theSelector, ...)• [self class] • theReceiver -> self • SEL -> class
  7. 7. objc_msgSendSuper• • id objc_msgSendSuper(struct objc_super *super, SEL op, ...)• [super class] • objc_super -> some struct? • SEL -> class
  8. 8. objc_superstruct objc_super { id receiver; Class superClass;}
  9. 9. objc_msgSendSuper• [super class] • objc_super receiver = self superClass = Base • objc_msgSendSuper objc_super -> superClass selector • selector objc_super -> receiver
  10. 10. That is ...• [self class] • objc_msgSend(receiver, class) • receiver = Derived, “class” selector is from NSObject• [super class] • objc_msgSendSuper(objc_super, class) • ==> objc_msgSend(objc_super -> receiver, class) • receiver = Derived, “class” selector is from NSObject• NSObject class receiver class
  11. 11. That is ...• [self class] • objc_msgSend(receiver, class) • receiver = Derived, class method is from NSObject• [super class] • objc_msgSendSuper(objc_super, class) So ... • [self class] ‘s output is Derived ==> objc_msgSend(objc_super -> receiver, class) class] ‘s output is Derived too ! [super • receiver = Derived, class method from NSObject• NSObject class receiver class
  12. 12. How to get super class’s name?• [self superclass]• NSLog(@"super class is %@", [self superclass]);• The output is “Base” !
  13. 13. The End

×