SlideShare une entreprise Scribd logo
1  sur  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/

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Dernier (20)

Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 

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/