SlideShare a Scribd company logo
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

DOCX
Dam31303 dti2143 lab sheet 7
PDF
Template matching
PDF
Software Engineering Best Practices @ Nylas
PDF
What's new and what's next in Electron & Chromium [2016]
PDF
Electron, databases, and RxDB
PDF
Building Native Experiences with Electron
PDF
what is_the_best_way_of_method_swizzling
PPTX
Objective C Tricks
Dam31303 dti2143 lab sheet 7
Template matching
Software Engineering Best Practices @ Nylas
What's new and what's next in Electron & Chromium [2016]
Electron, databases, and RxDB
Building Native Experiences with Electron
what is_the_best_way_of_method_swizzling
Objective C Tricks

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Artificial Intelligence
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PPT on Performance Review to get promotions
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf
OOP with Java - Java Introduction (Basics)
UNIT 4 Total Quality Management .pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CYBER-CRIMES AND SECURITY A guide to understanding
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Artificial Intelligence
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT on Performance Review to get promotions
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
R24 SURVEYING LAB MANUAL for civil enggi
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
Mechanical Engineering MATERIALS Selection
Ad
Ad

Black magic and swizzling in Objective-C

  • 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