Objective-C Runtime

3,463 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
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,463
On SlideShare
0
From Embeds
0
Number of Embeds
35
Actions
Shares
0
Downloads
108
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • who the hell am i?\nwho the hell are you?\n - proficent in objective - c\n - C experts\n
  • \n
  • what is objective - c\n - superset over C\n - designed to add Smalltalk like messaging and OOP in C\nwhich makes obj-c reflexive and dynamic which is accomplished through the runtime\n\n\n
  • what is objective - c\n - superset over C\n - designed to add Smalltalk like messaging and OOP in C\nwhich makes obj-c reflexive and dynamic which is accomplished through the runtime\n\n\n
  • what is objective - c\n - superset over C\n - designed to add Smalltalk like messaging and OOP in C\nwhich makes obj-c reflexive and dynamic which is accomplished through the runtime\n\n\n
  • objective-c leaves compiles some code but leaves many decisions until the code is executed\nso while c alone creates a top-down, largely immutable flow, objective c lets things get redirected and change dynamically\n
  • objective-c leaves compiles some code but leaves many decisions until the code is executed\nso while c alone creates a top-down, largely immutable flow, objective c lets things get redirected and change dynamically\n
  • objective-c leaves compiles some code but leaves many decisions until the code is executed\nso while c alone creates a top-down, largely immutable flow, objective c lets things get redirected and change dynamically\n
  • In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector. You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.\nImplementation - It is the memory address of the start of a code block that implements a Method.\n\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • make sure people know what these are...\n\ncode sample!!!!!!\n
  • make sure people know what these are...\n\ncode sample!!!!!!\n
  • make sure people know what these are...\n\ncode sample!!!!!!\n
  • make sure people know what these are...\n\ncode sample!!!!!!\n
  • \n
  • \n
  • \n
  • \n
  • The messaging function does everything necessary for dynamic binding:\nIt first finds the procedure (method implementation) that the selector refers to. Since the same method can be implemented differently by separate classes, the precise procedure that it finds depends on the class of the receiver.\nIt then calls the procedure, passing it the receiving object (a pointer to its data), along with any arguments that were specified for the method.\nFinally, it passes on the return value of the procedure as its own return value.\n\nMake sure people know what a SEL is\n\n
  • looks up the class hierarchy for the implementation\n\n
  • use code example for private apis \n
  • use code example for private apis \n
  • use code example for private apis \n
  • The Objective-C runtime lets you modify the mappings from a selector (method name) to an implementation (the method code itself). This allows you to "patch" methods in code you don't have the source to (AppKit,FoundationKit, etc). Unlike creating a category method with the same name as the original method (effectively replacing the original method), MethodSwizzling lets your replacement method make use of the original method, almost like subclassing.\n
  • dangers = if you don’t have access to the code you don’t know what assumptions are being made can cause instability\n changing UI elements from apple’s implementations to something else might make Apple angry and smash your app\n
  • dangers = if you don’t have access to the code you don’t know what assumptions are being made can cause instability\n changing UI elements from apple’s implementations to something else might make Apple angry and smash your app\n
  • dangers = if you don’t have access to the code you don’t know what assumptions are being made can cause instability\n changing UI elements from apple’s implementations to something else might make Apple angry and smash your app\n
  • \n
  • \n
  • \n
  • code demo\n
  • code demo\n
  • \n
  • \n
  • 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

    ×