SlideShare a Scribd company logo
1 of 12
Download to read offline
Black magic and swizzling in Objective-C
Me
iOS developer since 2008—first app, NetSketch
was on the App Store the day it went live.

Published more than 20 apps, which have been
featured on the App Store, demoed on stage at
SXSW, covered by Macworld.

Currently growing Kodiak, a platform that makes it
easy for you to build great educational apps.
5 minute agenda
Today, we’ll learn about Objective-C method
dispatching (and it’s beautiful simplicity.)

We’ll talk about Method Swizzling, a technique for
doing very bad things.

We’ll see an example of some really cool stuff you
can do with it.
Obj-C is a thin layer
Objective-C is a thin wrapper on top of C, and the
objc.h headers declare C functions that implement
method invocation, properties and more.
Objective-C calls can be translated into their C
counterparts:

[target	selector];	
objc_msgSend(target,	@selector(selector));
Methods in tables
Each class in Objective-C has
a lookup table associating
method names with selectors
(which point to functions.)

Objective-C categories are
simple: they allow you to add
methods to the lookup table.
You can add methods to
classes that already exist!
Method Swizzling
Categories let you add things to the method
lookup table, which is cool and somewhat unique
to Objective-C.

But what if I want to replace a method?

You can’t. Thanks for listening.
But I can write C!
@implementation NSString (Category)
- (NSString)betterDescription {
return @"Haha I win.";
}
@end
origMethod = class_getInstanceMethod([NSString class], @selector(description:));
altMethod = class_getInstanceMethod([NSString class], @selector(betterDescription:));
IMP temp = origMethod->method_imp;
origMethod->method_imp = altMethod->method_imp;
altMethod->method_imp = temp;
// What do you think this will do?
NSLog([@”Hello World” description]);
But... why?
Reroute calls to methodA: to methodB: instead.

Generally, you make methodB: call through to
methodA:, so the default behavior still exists.

Useful for logging, overriding behaviors, stubbing
methods for unit testing.

Use with extreme care. You will break things badly.
HTTP://DARKDUST.NET/WRITINGS/OBJECTIVE-C/METHOD-SWIZZLING
One use case: Spark
The Spark Inspector has been a side project of
mine for about a year, and is finally going through
App Store review.

Swizzles dozens of methods on core classes like
UIView to provide an awesome debug view.

Demo time! https://sparkinspector.com/
Secret Sauce
So how did that work? We added a framework to
our app and ran a line of code. What did that code
do?

The Spark Inspector swizzles setFrame:,
setNeedsDisplay: (and lots more), and sends
information about changes to a Mac app.

The mac app uses this information to rebuild the
view hierarchy.
Takeaways
Objective-C is a beautifully simple language, and
you can do some really powerful things with it.

In general, if you find yourself wanting to swizzle
something, you’re doing it wrong. But there are
great reasons to break the rules sometimes.

I’d be happy to send out free copies of the Spark
Inspector app once it’s live. Follow it on Twitter
@sparkinspector, and me @bengotow
http://cocoadev.com/wiki/MethodSwizzling

More Related Content

Similar to Obj-C Method Swizzling

classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
The Dark Side of Objective-C
The Dark Side of Objective-CThe Dark Side of Objective-C
The Dark Side of Objective-CMartin Kiss
 
Objective-C Runtime overview
Objective-C Runtime overviewObjective-C Runtime overview
Objective-C Runtime overviewFantageek
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock TutorialSbin m
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...NicheTech Com. Solutions Pvt. Ltd.
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.Abid Kohistani
 

Similar to Obj-C Method Swizzling (20)

Notepad tutorial
Notepad tutorialNotepad tutorial
Notepad tutorial
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
Objective c
Objective cObjective c
Objective c
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
The Dark Side of Objective-C
The Dark Side of Objective-CThe Dark Side of Objective-C
The Dark Side of Objective-C
 
Objective-C Runtime overview
Objective-C Runtime overviewObjective-C Runtime overview
Objective-C Runtime overview
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 
C# features
C# featuresC# features
C# features
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Iphone lecture imp
Iphone lecture  impIphone lecture  imp
Iphone lecture imp
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
C#/.NET Little Wonders
C#/.NET Little WondersC#/.NET Little Wonders
C#/.NET Little Wonders
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
OOP in C# Classes and Objects.
OOP in C# Classes and Objects.OOP in C# Classes and Objects.
OOP in C# Classes and Objects.
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
 

Recently uploaded

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 

Recently uploaded (20)

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

Obj-C Method Swizzling

  • 1. Black magic and swizzling in Objective-C
  • 2. Me iOS developer since 2008—first app, NetSketch was on the App Store the day it went live. Published more than 20 apps, which have been featured on the App Store, demoed on stage at SXSW, covered by Macworld. Currently growing Kodiak, a platform that makes it easy for you to build great educational apps.
  • 3. 5 minute agenda Today, we’ll learn about Objective-C method dispatching (and it’s beautiful simplicity.) We’ll talk about Method Swizzling, a technique for doing very bad things. We’ll see an example of some really cool stuff you can do with it.
  • 4. Obj-C is a thin layer Objective-C is a thin wrapper on top of C, and the objc.h headers declare C functions that implement method invocation, properties and more. Objective-C calls can be translated into their C counterparts: [target selector]; objc_msgSend(target, @selector(selector));
  • 5. Methods in tables Each class in Objective-C has a lookup table associating method names with selectors (which point to functions.) Objective-C categories are simple: they allow you to add methods to the lookup table. You can add methods to classes that already exist!
  • 6. Method Swizzling Categories let you add things to the method lookup table, which is cool and somewhat unique to Objective-C. But what if I want to replace a method? You can’t. Thanks for listening.
  • 7. But I can write C! @implementation NSString (Category) - (NSString)betterDescription { return @"Haha I win."; } @end origMethod = class_getInstanceMethod([NSString class], @selector(description:)); altMethod = class_getInstanceMethod([NSString class], @selector(betterDescription:)); IMP temp = origMethod->method_imp; origMethod->method_imp = altMethod->method_imp; altMethod->method_imp = temp; // What do you think this will do? NSLog([@”Hello World” description]);
  • 8. But... why? Reroute calls to methodA: to methodB: instead. Generally, you make methodB: call through to methodA:, so the default behavior still exists. Useful for logging, overriding behaviors, stubbing methods for unit testing. Use with extreme care. You will break things badly. HTTP://DARKDUST.NET/WRITINGS/OBJECTIVE-C/METHOD-SWIZZLING
  • 9. One use case: Spark The Spark Inspector has been a side project of mine for about a year, and is finally going through App Store review. Swizzles dozens of methods on core classes like UIView to provide an awesome debug view. Demo time! https://sparkinspector.com/
  • 10. Secret Sauce So how did that work? We added a framework to our app and ran a line of code. What did that code do? The Spark Inspector swizzles setFrame:, setNeedsDisplay: (and lots more), and sends information about changes to a Mac app. The mac app uses this information to rebuild the view hierarchy.
  • 11. Takeaways Objective-C is a beautifully simple language, and you can do some really powerful things with it. In general, if you find yourself wanting to swizzle something, you’re doing it wrong. But there are great reasons to break the rules sometimes. I’d be happy to send out free copies of the Spark Inspector app once it’s live. Follow it on Twitter @sparkinspector, and me @bengotow