SlideShare a Scribd company logo
1 of 16
Download to read offline
Method Swizzling
DMI Internal Tech Talk
Puth Sarangsey Long
iOS Software Engineer 25-May-2017
Swizzling

Optimizely
Reference

Demo

What we are talking about?
What ?

- Replacing the implementation
- Overriding default behaviour

- Dynamic method

- Changing the mapping between a specific
#selector(method)
Swizzling(1/8)
How to use ?

+ (void)load

- Notify when the class is initially loaded
+ (void)initialize

- Notify before the application calls its first
method on that class or an instance
+ dispatch_once

- swizzling changes global state

- code will be executed exactly once
Swizzling(2/8)
How to use ?

+ Selectors, Methods, & Implementations
- Selector (typedef struct objc_selector *SEL):

represent the name of a method at runtime,
selector is a C string that has been registered (or
“mapped”),
selectors generated by the compiler are automatically
mapped

- Method (typedef struct objc_method *Method):

represents a method in a class definition
Swizzling(3/8)
How to use ?

+ Selectors, Methods, & Implementations
- Implementation (typedef id (*IMP)(id, SEL, …)): 

a pointer to the start of the function that implements the
method,
first argument is a pointer to self,
second argument is the method selector
Swizzling(4/8)
Example

- Using selector & Implementations

+ (void)load
{
Method origMethod = class_getInstanceMethod(self,
@selector(drawTextInRect:));
origDrawTextInRect = (void *)method_getImplementation(origMethod);
if (!class_addMethod(self, @selector(drawTextInRect:),
(IMP)newDrawTextInRect, method_getTypeEncoding(origMethod)))
method_setImplementation(origMethod, (IMP)newDrawTextInRect);
}
Swizzling(5/8)
Example

- Using selector

+ (void)load
{
Method original, swizzle;
original = class_getInstanceMethod(self, @selector(initWithFrame:));
swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithFrame:));
// Swap their implementations.
method_exchangeImplementations(original, swizzle);
}
Swizzling(6/8)
Power of Swizzling

Swizzling(7/8)
optimizely
vwo
instapage
udacity
unbounce
JUSEmptyViewController
UIMenuItem-CXAImageSupport
ETNavBarTransparent
Realm
MJRefresh
google analytics
fabric
facebook analytics
Dangers of Method Swizzling

- Changes behaviour of un-owned code

- Possible naming conflicts

- Swizzling changes the method's arguments

- The order of swizzles

- Difficult to debug
Swizzling(8/8)
How?

- Method Swizzling
- Optimizely downloads a JSON data file
containing all experiment data
- An experiment conflicts with another when
you modify the same view, Live Variable, or
Code Block in multiple experiments.
Optimizely(1/3)
How?

- Disabled : the experiment has been paused
from the Optimizely dashboard
- Running: This means the experiment is
running and ready to be viewed.
- Deactivated: This means the experiment
failed a condition required to run
Optimizely(2/3)
Optimizely SDK work

Optimizely(3/3)
Reference
How-does-apptimize-optimizely-work-on-ios
Method swizzling
Extending existing classes
ABTest Prototype
Demo
View Border Color
UIMenuItem Image
ABTesting
Q&A

More Related Content

What's hot

storage class
storage classstorage class
storage class
student
 
Ensemble of Exemplar-SVM for Object Detection and Beyond
Ensemble of Exemplar-SVM for Object Detection and BeyondEnsemble of Exemplar-SVM for Object Detection and Beyond
Ensemble of Exemplar-SVM for Object Detection and Beyond
zukun
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
Jake Bond
 

What's hot (20)

Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
storage class
storage classstorage class
storage class
 
Java Method, Static Block
Java Method, Static BlockJava Method, Static Block
Java Method, Static Block
 
Storage class
Storage classStorage class
Storage class
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Ensemble of Exemplar-SVM for Object Detection and Beyond
Ensemble of Exemplar-SVM for Object Detection and BeyondEnsemble of Exemplar-SVM for Object Detection and Beyond
Ensemble of Exemplar-SVM for Object Detection and Beyond
 
Interface
InterfaceInterface
Interface
 
Java method
Java methodJava method
Java method
 
A Proposal to Orchestrate Test Cases
A Proposal to Orchestrate Test CasesA Proposal to Orchestrate Test Cases
A Proposal to Orchestrate Test Cases
 
Method of java
Method of javaMethod of java
Method of java
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話AnyObject – 自分が見落としていた、基本の話
AnyObject – 自分が見落としていた、基本の話
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Templates
TemplatesTemplates
Templates
 
Templates exception handling
Templates exception handlingTemplates exception handling
Templates exception handling
 
Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)Std 12 computer chapter 8 classes and object in java (part 2)
Std 12 computer chapter 8 classes and object in java (part 2)
 
Operators
OperatorsOperators
Operators
 
Unit iii
Unit iiiUnit iii
Unit iii
 

Similar to Method swizzling

polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
BapanKar2
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
DneprCiklumEvents
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
Terry Yoast
 
How to build an AOP framework in ActionScript
How to build an AOP framework in ActionScriptHow to build an AOP framework in ActionScript
How to build an AOP framework in ActionScript
Christophe Herreman
 

Similar to Method swizzling (20)

Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
polymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdfpolymorphismpresentation-160825122725.pdf
polymorphismpresentation-160825122725.pdf
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Design patterns: template method
Design patterns: template methodDesign patterns: template method
Design patterns: template method
 
البرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكالالبرمجة الهدفية بلغة جافا - تعدد الأشكال
البرمجة الهدفية بلغة جافا - تعدد الأشكال
 
Working with Methods in Java.pptx
Working with Methods in Java.pptxWorking with Methods in Java.pptx
Working with Methods in Java.pptx
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
 
Chap08
Chap08Chap08
Chap08
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
3 j unit
3 j unit3 j unit
3 j unit
 
Design patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjsDesign patterns in java script, jquery, angularjs
Design patterns in java script, jquery, angularjs
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
How to build an AOP framework in ActionScript
How to build an AOP framework in ActionScriptHow to build an AOP framework in ActionScript
How to build an AOP framework in ActionScript
 
Design patterns
Design patternsDesign patterns
Design patterns
 

Recently uploaded

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
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

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
 
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...
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
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
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
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...
 

Method swizzling

  • 1. Method Swizzling DMI Internal Tech Talk Puth Sarangsey Long iOS Software Engineer 25-May-2017
  • 3. What ?
 - Replacing the implementation - Overriding default behaviour
 - Dynamic method
 - Changing the mapping between a specific #selector(method) Swizzling(1/8)
  • 4. How to use ?
 + (void)load
 - Notify when the class is initially loaded + (void)initialize
 - Notify before the application calls its first method on that class or an instance + dispatch_once
 - swizzling changes global state
 - code will be executed exactly once Swizzling(2/8)
  • 5. How to use ?
 + Selectors, Methods, & Implementations - Selector (typedef struct objc_selector *SEL):
 represent the name of a method at runtime, selector is a C string that has been registered (or “mapped”), selectors generated by the compiler are automatically mapped
 - Method (typedef struct objc_method *Method):
 represents a method in a class definition Swizzling(3/8)
  • 6. How to use ?
 + Selectors, Methods, & Implementations - Implementation (typedef id (*IMP)(id, SEL, …)): 
 a pointer to the start of the function that implements the method, first argument is a pointer to self, second argument is the method selector Swizzling(4/8)
  • 7. Example
 - Using selector & Implementations
 + (void)load { Method origMethod = class_getInstanceMethod(self, @selector(drawTextInRect:)); origDrawTextInRect = (void *)method_getImplementation(origMethod); if (!class_addMethod(self, @selector(drawTextInRect:), (IMP)newDrawTextInRect, method_getTypeEncoding(origMethod))) method_setImplementation(origMethod, (IMP)newDrawTextInRect); } Swizzling(5/8)
  • 8. Example
 - Using selector
 + (void)load { Method original, swizzle; original = class_getInstanceMethod(self, @selector(initWithFrame:)); swizzle = class_getInstanceMethod(self, @selector(swizzled_initWithFrame:)); // Swap their implementations. method_exchangeImplementations(original, swizzle); } Swizzling(6/8)
  • 10. Dangers of Method Swizzling
 - Changes behaviour of un-owned code
 - Possible naming conflicts
 - Swizzling changes the method's arguments
 - The order of swizzles
 - Difficult to debug Swizzling(8/8)
  • 11. How?
 - Method Swizzling - Optimizely downloads a JSON data file containing all experiment data - An experiment conflicts with another when you modify the same view, Live Variable, or Code Block in multiple experiments. Optimizely(1/3)
  • 12. How?
 - Disabled : the experiment has been paused from the Optimizely dashboard - Running: This means the experiment is running and ready to be viewed. - Deactivated: This means the experiment failed a condition required to run Optimizely(2/3)
  • 16. Q&A