Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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, w...
5 minute agenda
Today, we’ll learn about Objective-C method
dispatching (and it’s beautiful simplicity.)

We’ll talk about...
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...
Methods in tables
Each class in Objective-C has
a lookup table associating
method names with selectors
(which point to fun...
Method Swizzling
Categories let you add things to the method
lookup table, which is cool and somewhat unique
to Objective-...
But I can write C!
@implementation NSString (Category)
- (NSString)betterDescription {
return @"Haha I win.";
}
@end
origM...
But... why?
Reroute calls to methodA: to methodB: instead.

Generally, you make methodB: call through to
methodA:, so the ...
One use case: Spark
The Spark Inspector has been a side project of
mine for about a year, and is finally going through
App ...
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 Spa...
Takeaways
Objective-C is a beautifully simple language, and
you can do some really powerful things with it.

In general, i...
http://cocoadev.com/wiki/MethodSwizzling
Upcoming SlideShare
Loading in …5
×

0

Share

Download to read offline

Black magic and swizzling in Objective-C

Download to read offline

A quick deep dive into method swizzling, one of Objective-C's most powerful tricks.

Related Books

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Black magic and swizzling in Objective-C

  1. 1. Black magic and swizzling in Objective-C
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  12. 12. http://cocoadev.com/wiki/MethodSwizzling

A quick deep dive into method swizzling, one of Objective-C's most powerful tricks.

Views

Total views

450

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

4

Shares

0

Comments

0

Likes

0

×