Objective-C Runtime
Upcoming SlideShare
Loading in...5
×
 

Objective-C Runtime

on

  • 2,124 views

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

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

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

Statistics

Views

Total Views
2,124
Views on SlideShare
2,112
Embed Views
12

Actions

Likes
2
Downloads
69
Comments
0

2 Embeds 12

https://www.linkedin.com 8
http://www.linkedin.com 4

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 Objective-C Runtime Presentation Transcript

  • the objective-c runtime ryan johnsonryan@mds.tc @_ryanjohnson_
  • enough about me,lets talk about you.
  • what is objective-c? View slide
  • what is objective-c?objc = C + smalltalk View slide
  • what is objective-c?objc = C + smalltalkobjc = C + messaging + OOP
  • what is objective-c?objc = C + smalltalkobjc = C + messaging + OOPobjective c is dynamic, and that’s as awesome as it sounds.
  • so, what’s the runtime?
  • so, what’s the runtime?C and assembly library which adds oop
  • so, what’s the runtime?C and assembly library which adds oopallows objects to be aware of and manipulate their own state
  • 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)
  • definitions• selector @selector - name of the method SEL - compiled selector, unique identifier that replaces the name• invocation (IMP) - what actually performs the magic
  • before using anything you learn here
  • 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.
  • better ways to accomplish things
  • better ways to accomplish things• categories
  • better ways to accomplish things• categories• subclasses
  • better ways to accomplish things• categories• subclasses• protocols
  • better ways to accomplish things• categories• subclasses• protocols that’s great, but I was promised a talk about the runtime you jerk.
  • highway to the danger zone
  • highway to the danger zoneusing the runtime functions isn’t going to get you rejected
  • highway to the danger zoneusing the runtime functions isn’t going to get you rejected but.....
  • 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
  • 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
  • messagingwhen you write: [foo bar:variable];the compiler turns it into: objc_msg(foo, @selector(bar:),variable);
  • 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
  • _abusing objc_msgSend
  • _ abusing objc_msgSend• calling private apis - useful for debugging
  • _ abusing objc_msgSend• calling private apis - useful for debugging• avoiding annoying ARC performSelector: warnings.
  • _ abusing objc_msgSend• calling private apis - useful for debugging• avoiding annoying ARC performSelector: warnings.• looking bad ass
  • method swizziling• in short, changing what the selector maps to• useful when a single method needs replacement, and doesn’t warrant subclassing
  • how and when to swizzle
  • how and when to swizzle• when customizing UIKit, but might get you rejected :( mock objects for unit tests - (OCMock)
  • 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.
  • 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
  • associated objects
  • associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass
  • associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  • associated objectsgreat way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  • using associated objects
  • 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
  • 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
  • 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
  • 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