8. so, what’s the runtime?
C and assembly library which adds oop
9. so, what’s the runtime?
C and assembly library which adds oop
allows objects to be aware of and manipulate their own state
10. so, what’s the runtime?
C and assembly library which adds oop
allows objects to be aware of and manipulate their own state
two runtimes: modern (we all use this) and legacy (32-bit)
11. definitions
• selector
@selector - name of the method
SEL - compiled selector, unique identifier that replaces the name
• invocation (IMP) - what actually performs the magic
13. before using anything
you learn here
ask yourself
is 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.
20. highway to the danger zone
using the runtime functions isn’t going to get you rejected
21. highway to the danger zone
using the runtime functions isn’t going to get you rejected
but.....
22. 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 will
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 will
you may be smart, but your idea to call a private method with
objc_msgSend isn’t
24. messaging
when you write:
[foo bar:variable];
the compiler turns it into:
objc_msg(foo, @selector(bar:),variable);
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
32. how and when to swizzle
• when
customizing UIKit, but might get you rejected :(
mock objects for unit tests - (OCMock)
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. 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
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. 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. 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. 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
Notes de l'éditeur
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&#x2019;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&#x2014;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&#x2019;t have access to the code you don&#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&#x2019;s implementations to something else might make Apple angry and smash your app\n
dangers = if you don&#x2019;t have access to the code you don&#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&#x2019;s implementations to something else might make Apple angry and smash your app\n
dangers = if you don&#x2019;t have access to the code you don&#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&#x2019;s implementations to something else might make Apple angry and smash your app\n