Hacking Cocoa: Method Swizzling

blog — Tags: , , , , , , , — anon @ 10/03 5:47 pm

From cocoadev.com:

The Objective-C runtime lets you modify the mappings from a selector (method name) to an implementation (the method code itself). This allows you to “patch” methods in code you don’t have the source to (AppKit, FoundationKit, etc).

Unlike creating a category method with the same name as the original method (effectively replacing the original method), MethodSwizzling lets your replacement method make use of the original method, almost like subclassing.

This is best used in cases where a single method needs substitution or extension but if you need to modify many behaviors of a class, you may be better off using ClassPosing.

But I found this article way more informative and that, in turn, led my way to the SIMBL Framework which I can greatly recommend.

After thinking about this a couple of minutes you’ll also find a huge amount of possibilities and hack-arounds. But seriously, there are some things to consider like this.

To make it short, Kevin Ballards code did fail in the 10.5 Mac OS X release and had to be rewritten. In fact, many code had to be rewritten which somehow led to JRSwizzle, a very good implementation that runs under Leopard and Snow Leopard.

So either using SIMBL or MethodSwizzling (JRSwizzle in particular) does the trick. It’s absolutely obvious to be very careful when swizzling methods or creating plugins via SIMBL – but the possibilities appear very clear and vast to me.

Another great possibility to hack around with Cocoa is F-Script by Philippe Mougin and F-ScriptAnywhere by Ken Ferry. Two very essential tools when hacking just for fun or seriously developing Cocoa applications.

copyright © 2008-2010 datenkompost.de/blog - barecity derivative | imprint
* a title remix inspired by a popular german book