SlideShare una empresa de Scribd logo
1 de 43
the objective-c runtime
                      ryan johnson




ryan@mds.tc                             @_ryanjohnson_
enough about me,
lets talk about you.
what is objective-c?
what is objective-c?


objc = C + smalltalk
what is objective-c?


objc = C + smalltalk

objc = C + messaging + OOP
what is objective-c?


objc = C + smalltalk

objc = C + messaging + OOP

objective 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 oop

allows objects to be aware of and manipulate their own state
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)
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 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.
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 zone

using the runtime functions isn’t going to get you rejected
highway to the danger zone

using the runtime functions isn’t going to get you rejected

                          but.....
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
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
messaging

when 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 objects

great way to add instance variables to a class you don’t own or is
                       difficult to subclass
associated objects

great way to add instance variables to a class you don’t own or is
                       difficult to subclass

                       and, it’s arc friendly.
associated objects

great 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

Más contenido relacionado

Último

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Objective-C Runtime

  • 1. the objective-c runtime ryan johnson ryan@mds.tc @_ryanjohnson_
  • 2. enough about me, lets talk about you.
  • 4. what is objective-c? objc = C + smalltalk
  • 5. what is objective-c? objc = C + smalltalk objc = C + messaging + OOP
  • 6. what is objective-c? objc = C + smalltalk objc = C + messaging + OOP objective c is dynamic, and that’s as awesome as it sounds.
  • 7. so, what’s the runtime?
  • 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
  • 12. before using anything you learn here
  • 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.
  • 14. better ways to accomplish things
  • 15. better ways to accomplish things • categories
  • 16. better ways to accomplish things • categories • subclasses
  • 17. better ways to accomplish things • categories • subclasses • protocols
  • 18. better ways to accomplish things • categories • subclasses • protocols that’s great, but I was promised a talk about the runtime you jerk.
  • 19. highway to the danger zone
  • 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
  • 27. _ abusing objc_msgSend • calling private apis - useful for debugging
  • 28. _ abusing objc_msgSend • calling private apis - useful for debugging • avoiding annoying ARC performSelector: warnings.
  • 29. _ abusing objc_msgSend • calling private apis - useful for debugging • avoiding annoying ARC performSelector: warnings. • looking bad ass
  • 30. method swizziling • in short, changing what the selector maps to • useful when a single method needs replacement, and doesn’t warrant subclassing
  • 31. how and when to swizzle
  • 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
  • 36. associated objects great way to add instance variables to a class you don’t own or is difficult to subclass
  • 37. associated objects great way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  • 38. associated objects great way to add instance variables to a class you don’t own or is difficult to subclass and, it’s arc friendly.
  • 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

Notas del editor

  1. who the hell am i?\nwho the hell are you?\n - proficent in objective - c\n - C experts\n
  2. \n
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it&amp;#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&amp;#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
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. make sure people know what these are...\n\ncode sample!!!!!!\n
  16. make sure people know what these are...\n\ncode sample!!!!!!\n
  17. make sure people know what these are...\n\ncode sample!!!!!!\n
  18. make sure people know what these are...\n\ncode sample!!!!!!\n
  19. \n
  20. \n
  21. \n
  22. \n
  23. 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
  24. looks up the class hierarchy for the implementation\n\n
  25. use code example for private apis \n
  26. use code example for private apis \n
  27. use code example for private apis \n
  28. 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 &quot;patch&quot; methods in code you don&apos;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
  29. dangers = if you don&amp;#x2019;t have access to the code you don&amp;#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&amp;#x2019;s implementations to something else might make Apple angry and smash your app\n
  30. dangers = if you don&amp;#x2019;t have access to the code you don&amp;#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&amp;#x2019;s implementations to something else might make Apple angry and smash your app\n
  31. dangers = if you don&amp;#x2019;t have access to the code you don&amp;#x2019;t know what assumptions are being made can cause instability\n changing UI elements from apple&amp;#x2019;s implementations to something else might make Apple angry and smash your app\n
  32. \n
  33. \n
  34. \n
  35. code demo\n
  36. code demo\n
  37. \n
  38. \n