Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Objective-C Runtime

3,913 views

Published on

Presentation given to the MN iOS Developer Meetup in July 2012.

Related Code Can Be Found Here:
https://gist.github.com/3150536

Published in: Technology
  • Be the first to comment

Objective-C Runtime

  1. 1. the objective-c runtime ryan johnsonryan@mds.tc @_ryanjohnson_
  2. 2. enough about me,lets talk about you.
  3. 3. what is objective-c?
  4. 4. what is objective-c?objc = C + smalltalk
  5. 5. what is objective-c?objc = C + smalltalkobjc = C + messaging + OOP
  6. 6. what is objective-c?objc = C + smalltalkobjc = C + messaging + OOPobjective c is dynamic, and that’s as awesome as it sounds.
  7. 7. so, what’s the runtime?
  8. 8. so, what’s the runtime?C and assembly library which adds oop
  9. 9. so, what’s the runtime?C and assembly library which adds oopallows objects to be aware of and manipulate their own state
  10. 10. so, what’s the runtime?C and assembly library which adds oopallows objects to be aware of and manipulate their own statetwo runtimes: modern (we all use this) and legacy (32-bit)
  11. 11. definitions• selector @selector - name of the method SEL - compiled selector, unique identifier that replaces the name• invocation (IMP) - what actually performs the magic
  12. 12. before using anything you learn here
  13. 13. before using anything you learn here ask yourselfis this the easiest, most elegant way to achieve my goals? it’s probably not. you don’t get a gold star for over engineering, and no one is impressed by needlessly complicated code.
  14. 14. better ways to accomplish things
  15. 15. better ways to accomplish things• categories
  16. 16. better ways to accomplish things• categories• subclasses
  17. 17. better ways to accomplish things• categories• subclasses• protocols
  18. 18. better ways to accomplish things• categories• subclasses• protocols that’s great, but I was promised a talk about the runtime you jerk.
  19. 19. highway to the danger zone
  20. 20. highway to the danger zoneusing the runtime functions isn’t going to get you rejected
  21. 21. highway to the danger zoneusing the runtime functions isn’t going to get you rejected but.....
  22. 22. highway to the danger zoneusing the runtime functions isn’t going to get you rejected but.....using them the wrong way or to do prohibited things will
  23. 23. highway to the danger zone using the runtime functions isn’t going to get you rejected but..... using them the wrong way or to do prohibited things willyou may be smart, but your idea to call a private method with objc_msgSend isn’t
  24. 24. messagingwhen you write: [foo bar:variable];the compiler turns it into: objc_msg(foo, @selector(bar:),variable);
  25. 25. what objc_msgSend does• finds the implementation, calling the procedure and returning the value.• also, passes hidden arguments: self - the receiver of the message _cmd - the @selector for the method
  26. 26. _abusing objc_msgSend
  27. 27. _ abusing objc_msgSend• calling private apis - useful for debugging
  28. 28. _ abusing objc_msgSend• calling private apis - useful for debugging• avoiding annoying ARC performSelector: warnings.
  29. 29. _ abusing objc_msgSend• calling private apis - useful for debugging• avoiding annoying ARC performSelector: warnings.• looking bad ass
  30. 30. method swizziling• in short, changing what the selector maps to• useful when a single method needs replacement, and doesn’t warrant subclassing
  31. 31. how and when to swizzle
  32. 32. how and when to swizzle• when customizing UIKit, but might get you rejected :( mock objects for unit tests - (OCMock)
  33. 33. how and when to swizzle• when customizing UIKit, but might get you rejected :( mock objects for unit tests - (OCMock)• other options: categories allow you to replace methods completely but can’t call original implementation and not guaranteed your code will be called.
  34. 34. how and when to swizzle• when customizing UIKit, but might get you rejected :( mock objects for unit tests - (OCMock)• other options: categories allow you to replace methods completely but can’t call original implementation and not guaranteed your code will be called.• dangers
  35. 35. associated objects
  36. 36. associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass
  37. 37. associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  38. 38. associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  39. 39. using associated objects
  40. 40. using associated objects• setting objc_setAssociatedObject(object, key, value, policy); id object <- the target for the association void * key <- the key(name) of the association - e.g. “type” id value <- the object which will be associated with the target objc_AssociationPolicy policy <- the memory management policy to use options: OBJC_ASSOCIATION_ASSIGN, OBJC_ASSOCIATION_RETAIN_NONATOMIC, OBJC_ASSOCIATION_COPY_NONATOMIC, OBJC_ASSOCIATION_RETAIN, OBJC_ASSOCIATION_COPY
  41. 41. using associated objects• setting objc_setAssociatedObject(object, key, value, policy); id object <- the target for the association void * key <- the key(name) of the association - e.g. “type” id value <- the object which will be associated with the target objc_AssociationPolicy policy <- the memory management policy to use options: OBJC_ASSOCIATION_ASSIGN, OBJC_ASSOCIATION_RETAIN_NONATOMIC, OBJC_ASSOCIATION_COPY_NONATOMIC, OBJC_ASSOCIATION_RETAIN, OBJC_ASSOCIATION_COPY• getting objc_getAssociatedObject(object, key); id object <- source object for the association void * key <- the key(name) of the association
  42. 42. learning is fun. Slightly beyond our scope but check these out on your own:• NSProxy - can create ‘placeholder’ objects• NSInvocation - objectifies messages, used a lot by NSTimer
  43. 43. resources• Objective-C Runtime Programing Guide https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/ Introduction/Introduction.html#//apple_ref/doc/uid/TP40008048-CH1-SW1• Mike Ash’s Blog http://www.mikeash.com• Code Samurai http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html

×