SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Disclaimer
This talk is mostly for educational purposes.
⚠
A bit on the Swift runtime.
It’s turtles, all the way down.
• Apple platforms utilise the Objective-C runtime, Swift runs on top of this (iOS 7,
macOS 10.11+).

• Linux (and other platforms) don’t use this runtime. They ship with a “corelibs”
library that creates a runtime for Swift, but it is much smaller and doesn’t
support Objective-C.

• So only on Apple platforms can you inter-op Swift & Objective-C.

• But that also means Swift also has access to the same runtime environment as
Objective-C! (Through ObjectiveC - A swift library present on Apple systems).

• Yay! (uh-oh)
OOP
We made Alan Kay very sad (Please watch! https://www.youtube.com/watch?v=NdSD07U5uBs)
• OOP was originally designed to mimic natural biological systems.

• You have an object and it has properties. How do you find those properties?
You talk to the object. And if it understands you, it will respond.

• Objective-C is closer to the original design of OOP when compared to Java, C+
+, Pure Swift.

• objc_msgSend(id, SEL, …args) -> id* is truly dynamic.
• * This is no longer the signature due to ABI, but it sort of is as it’s
implemented in assembly. -> https://www.mikeash.com/pyblog/
objc_msgsends-new-prototype.html
A Smalltalk object can do exactly three things: 

• Hold state (references to other objects).

• Receive a message from itself or another object.

• In the course of processing a message, send messages to itself or another object.

The state an object holds is always private to that object. Other objects can query or
change that state only by sending requests (messages) to the object to do so. Any
message can be sent to any object: when a message is received, the receiver determines
whether that message is appropriate. Alan Kay has commented that despite the attention
given to objects, messaging is the most important concept in Smalltalk: "The big idea is
'messaging'—that is what the kernel of Smalltalk/Squeak is all about
Objectively speaking
Cells vs Gears
Fluidity vs Rigidity
objc_msgSend(id, SEL, …args)
A thingy A message It’s caveats
objc_msgSend(self, viewDidAppear, [animated:true]);
Hello Mr.ViewController. Could you appear please? And when you do, could you
animate?
Monkey Patching
🐒
• The Objective-C runtime allows you to rebind the way an object will respond
to messages.

• This is known as Swizzling.

• Apparently no one remembers where that name came from.
I’m going to say uh-oh again.
The possibilities are frightening.
• ViewControllers that actually call viewDidDisappear when you call viewDidAppear.

• Additions turning to subtraction.

• All of your data getting slurped up on applicationDidLaunch…Hello Google and Facebook
SDKs!

• Facebook Swizzles didMoveToWindow, setDelegate.

• https://github.com/facebook/facebook-ios-sdk/search?
q=FBSDKSwizzler&type=code

• Google Swizzles viewDidAppear, viewDidDisappear:

• https://github.com/firebase/firebase-ios-sdk/search?q=swizzleClass
AOP
No, not Air Observation Post. Aspect orientated programming!
• Non business logic (non functional requirements) are concerns that need “weaved” into your application.

• Common use cases; Logging, Security, Profiling, Transaction Management, Analytics.

• Really useful for adding an “aspect” of functionality to your app. i.e your app doesn’t have logging at the
moment, but you’ve got 600 screens and you want to add it to all of them.

• Solves a few problems;

• Reduces boilerplate.

• Keeps unrelated code separated from business logic.

• Dynamic application of function. Turn aspect on/off based on flags.

• Allows you to open closed interfaces (uh-oh x3).
Demo
Further reading
An evolutionary dead end, but an interesting one all the same.
• Anything by Alan Kay or Joe Armstrong on OOP

• https://medium.com/chmcore/a-short-history-of-objective-c-aff9d2bde8dd

• Akka Actors show how message passing works even in a networked system.

• https://doc.akka.io/docs/akka/current/typed/actors.html#akka-actors

• https://www.mikeash.com/pyblog/objc_msgsends-new-prototype.html

• https://indiestack.com/2019/10/casting-objective-c-message-sends/

Weitere ähnliche Inhalte

Was ist angesagt?

iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & Navigation
Marian Ignev
 
Make Mobile Apps Quickly
Make Mobile Apps QuicklyMake Mobile Apps Quickly
Make Mobile Apps Quickly
Gil Irizarry
 
Contributing to the Community
Contributing to the CommunityContributing to the Community
Contributing to the Community
WO Community
 
"Walk in a distributed systems park with Orleans" Евгений Бобров
"Walk in a distributed systems park with Orleans" Евгений Бобров"Walk in a distributed systems park with Orleans" Евгений Бобров
"Walk in a distributed systems park with Orleans" Евгений Бобров
Fwdays
 

Was ist angesagt? (20)

Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
Avoid memory leaks using unit tests - Swift Delhi Meetup - Chapter 15
 
Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns
 
iOS Memory management & Navigation
iOS Memory management & NavigationiOS Memory management & Navigation
iOS Memory management & Navigation
 
Promoter – A Python Project for Replicating a JSS via the API
Promoter – A Python Project for Replicating a JSS via the APIPromoter – A Python Project for Replicating a JSS via the API
Promoter – A Python Project for Replicating a JSS via the API
 
Actor model : A Different Concurrency Approach
Actor model : A Different Concurrency ApproachActor model : A Different Concurrency Approach
Actor model : A Different Concurrency Approach
 
Make Mobile Apps Quickly
Make Mobile Apps QuicklyMake Mobile Apps Quickly
Make Mobile Apps Quickly
 
Contributing to the Community
Contributing to the CommunityContributing to the Community
Contributing to the Community
 
The Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better Concurrency
 
Search and analyze your data with elasticsearch
Search and analyze your data with elasticsearchSearch and analyze your data with elasticsearch
Search and analyze your data with elasticsearch
 
Building applications with akka.net
Building applications with akka.netBuilding applications with akka.net
Building applications with akka.net
 
Building a Lightweight Discovery Interface for China's Patents@NYC Solr/Lucen...
Building a Lightweight Discovery Interface for China's Patents@NYC Solr/Lucen...Building a Lightweight Discovery Interface for China's Patents@NYC Solr/Lucen...
Building a Lightweight Discovery Interface for China's Patents@NYC Solr/Lucen...
 
Actor model in .NET - Akka.NET
Actor model in .NET - Akka.NETActor model in .NET - Akka.NET
Actor model in .NET - Akka.NET
 
"Walk in a distributed systems park with Orleans" Евгений Бобров
"Walk in a distributed systems park with Orleans" Евгений Бобров"Walk in a distributed systems park with Orleans" Евгений Бобров
"Walk in a distributed systems park with Orleans" Евгений Бобров
 
Creating scalable message driven solutions akkadotnet
Creating scalable message driven solutions akkadotnetCreating scalable message driven solutions akkadotnet
Creating scalable message driven solutions akkadotnet
 
Massively Scaleable .NET Web Services with Project Orleans
Massively Scaleable .NET Web Services with Project OrleansMassively Scaleable .NET Web Services with Project Orleans
Massively Scaleable .NET Web Services with Project Orleans
 
CQRS + ES with Scala and Akka
CQRS + ES with Scala and AkkaCQRS + ES with Scala and Akka
CQRS + ES with Scala and Akka
 
Memory Management in RubyMotion
Memory Management in RubyMotionMemory Management in RubyMotion
Memory Management in RubyMotion
 
Actor Patterns and NATS - Boulder Meetup
Actor Patterns and NATS - Boulder MeetupActor Patterns and NATS - Boulder Meetup
Actor Patterns and NATS - Boulder Meetup
 
A gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor modelA gentle introduction into AKKA and the actor model
A gentle introduction into AKKA and the actor model
 
Akka Fundamentals
Akka FundamentalsAkka Fundamentals
Akka Fundamentals
 

Ähnlich wie Method Swizzling with Objective-C

Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
DataArt
 
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open WideOCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
OCCIware
 
Cara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
Cara Tepat Menjadi iOS Developer Expert - Gilang RamadhanCara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
Cara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
DicodingEvent
 

Ähnlich wie Method Swizzling with Objective-C (20)

Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
Intro to iOS: Object Oriented Programming and Objective-C
Intro to iOS: Object Oriented Programming and Objective-CIntro to iOS: Object Oriented Programming and Objective-C
Intro to iOS: Object Oriented Programming and Objective-C
 
Tech breakfast 18
Tech breakfast 18Tech breakfast 18
Tech breakfast 18
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1LEARNING	 iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
 
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
Make Cross-platform Mobile Apps Quickly - SIGGRAPH 2014
 
EclipseCon 2016 - OCCIware : one Cloud API to rule them all
EclipseCon 2016 - OCCIware : one Cloud API to rule them allEclipseCon 2016 - OCCIware : one Cloud API to rule them all
EclipseCon 2016 - OCCIware : one Cloud API to rule them all
 
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open WideOCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
OCCIware Project at EclipseCon France 2016, by Marc Dutoo, Open Wide
 
Ios development
Ios developmentIos development
Ios development
 
Understanding iOS from an Android perspective
Understanding iOS from an Android perspectiveUnderstanding iOS from an Android perspective
Understanding iOS from an Android perspective
 
Cara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
Cara Tepat Menjadi iOS Developer Expert - Gilang RamadhanCara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
Cara Tepat Menjadi iOS Developer Expert - Gilang Ramadhan
 
Jailbreak Detector Detector
Jailbreak Detector DetectorJailbreak Detector Detector
Jailbreak Detector Detector
 
Spring session
Spring sessionSpring session
Spring session
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
DWX 2013 Nuremberg
DWX 2013 NurembergDWX 2013 Nuremberg
DWX 2013 Nuremberg
 
RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)RubyMotion Inspect Conference - 2013. (Without speaker notes.)
RubyMotion Inspect Conference - 2013. (Without speaker notes.)
 
Android programming-basics
Android programming-basicsAndroid programming-basics
Android programming-basics
 
30 days or less: New Features to Production
30 days or less: New Features to Production30 days or less: New Features to Production
30 days or less: New Features to Production
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
Giving back with GitHub - Putting the Open Source back in iOS
Giving back with GitHub - Putting the Open Source back in iOSGiving back with GitHub - Putting the Open Source back in iOS
Giving back with GitHub - Putting the Open Source back in iOS
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
[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
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Method Swizzling with Objective-C

  • 1. Disclaimer This talk is mostly for educational purposes. ⚠
  • 2. A bit on the Swift runtime. It’s turtles, all the way down. • Apple platforms utilise the Objective-C runtime, Swift runs on top of this (iOS 7, macOS 10.11+). • Linux (and other platforms) don’t use this runtime. They ship with a “corelibs” library that creates a runtime for Swift, but it is much smaller and doesn’t support Objective-C. • So only on Apple platforms can you inter-op Swift & Objective-C. • But that also means Swift also has access to the same runtime environment as Objective-C! (Through ObjectiveC - A swift library present on Apple systems). • Yay! (uh-oh)
  • 3. OOP We made Alan Kay very sad (Please watch! https://www.youtube.com/watch?v=NdSD07U5uBs) • OOP was originally designed to mimic natural biological systems. • You have an object and it has properties. How do you find those properties? You talk to the object. And if it understands you, it will respond. • Objective-C is closer to the original design of OOP when compared to Java, C+ +, Pure Swift. • objc_msgSend(id, SEL, …args) -> id* is truly dynamic. • * This is no longer the signature due to ABI, but it sort of is as it’s implemented in assembly. -> https://www.mikeash.com/pyblog/ objc_msgsends-new-prototype.html
  • 4.
  • 5. A Smalltalk object can do exactly three things: • Hold state (references to other objects). • Receive a message from itself or another object. • In the course of processing a message, send messages to itself or another object. The state an object holds is always private to that object. Other objects can query or change that state only by sending requests (messages) to the object to do so. Any message can be sent to any object: when a message is received, the receiver determines whether that message is appropriate. Alan Kay has commented that despite the attention given to objects, messaging is the most important concept in Smalltalk: "The big idea is 'messaging'—that is what the kernel of Smalltalk/Squeak is all about Objectively speaking
  • 7.
  • 8.
  • 9. objc_msgSend(id, SEL, …args) A thingy A message It’s caveats objc_msgSend(self, viewDidAppear, [animated:true]); Hello Mr.ViewController. Could you appear please? And when you do, could you animate?
  • 10. Monkey Patching 🐒 • The Objective-C runtime allows you to rebind the way an object will respond to messages. • This is known as Swizzling. • Apparently no one remembers where that name came from.
  • 11. I’m going to say uh-oh again. The possibilities are frightening. • ViewControllers that actually call viewDidDisappear when you call viewDidAppear. • Additions turning to subtraction. • All of your data getting slurped up on applicationDidLaunch…Hello Google and Facebook SDKs! • Facebook Swizzles didMoveToWindow, setDelegate. • https://github.com/facebook/facebook-ios-sdk/search? q=FBSDKSwizzler&type=code • Google Swizzles viewDidAppear, viewDidDisappear: • https://github.com/firebase/firebase-ios-sdk/search?q=swizzleClass
  • 12. AOP No, not Air Observation Post. Aspect orientated programming! • Non business logic (non functional requirements) are concerns that need “weaved” into your application. • Common use cases; Logging, Security, Profiling, Transaction Management, Analytics. • Really useful for adding an “aspect” of functionality to your app. i.e your app doesn’t have logging at the moment, but you’ve got 600 screens and you want to add it to all of them. • Solves a few problems; • Reduces boilerplate. • Keeps unrelated code separated from business logic. • Dynamic application of function. Turn aspect on/off based on flags. • Allows you to open closed interfaces (uh-oh x3).
  • 13.
  • 14. Demo
  • 15. Further reading An evolutionary dead end, but an interesting one all the same. • Anything by Alan Kay or Joe Armstrong on OOP • https://medium.com/chmcore/a-short-history-of-objective-c-aff9d2bde8dd • Akka Actors show how message passing works even in a networked system. • https://doc.akka.io/docs/akka/current/typed/actors.html#akka-actors • https://www.mikeash.com/pyblog/objc_msgsends-new-prototype.html • https://indiestack.com/2019/10/casting-objective-c-message-sends/