• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Streamlined way to swizzle Objective-C instance and class methods

ZKSwizzle

Streamlined way to swizzle Objective-C instance and class methods. ZKSwizzle makes swizzling instance and class methods of an Objective-C class as declarative as possible. You define a new class and implement all the methods you want to swizzle/add and then call [ZKSwizzle swizzleClass: forClass:] and any calls to the target class will be instead routed to your own.

ZKSwizzle also provides macros for calling the original implementation if need be and for calling the implementation of the superclass of the swizzled class. Enough talk, let's get crackin':

@interface OriginalObject : NSObject
@end
		
// Define a class which we will swizzle
@implementation OriginalObject
+ (BOOL)isSubclassOfClass:(Class)aClass { return YES; }
+ (NSString *)classMethod { return @"original"; }
+ (NSString *)description { return @"original"; }
- (NSString *)instanceMethod { return @"original"; }
- (NSString *)description { return @"original"; }
@end
		
// All methods on this class which are present on the class that
// it is swizzled to (including superclasses) are called instead of their
// original implementation. The original implementaion can be accessed with the 
// _orig(TYPE, ...) macro and the implementation of the superclass of the class which
// it was swizzled to can be access with the _super(TYPE, ...) macro
// hook(TargetClass) defines a class for
// you that will get swizzled automatically on launch with the TargetClass
hook(OriginalObject)
// Returns YES
+ (BOOL)isSubclassOfClass:(Class)aClass { return _orig(BOOL); }

// Returns "original_replaced"
- (NSString *)className { return [_orig(NSString *) stringByAppendingString:@"_replaced"]; }

// Returns "replaced" when called on the OriginalObject class
+ (NSString *)classMethod { return @"replaced"; }

// Returns the default description implemented by NSObject
+ (NSString *)description { return _super(NSString *); }

// Returns "replaced" when called on an instance of OriginalObject
- (NSString *)instanceMethod { return @"replaced"; }
	
// Returns the default description implemented by NSObject
- (NSString *)description { return _super(NSString *); }
	
// This method is added to instances of OriginalObject and can be called
// like any normal function on OriginalObject
- (void)addedMethod { NSLog(@"this method was added to OriginalObject"); }
endhook

Call this somewhere to initialize the swizzling:

// ZKSwizzle(SOURCE, DST) is a macro shorthand for calling 
// ZKSwizzleInterface handles this step for you, but you will
// have to call it manually if you don't use ZKSwizzleInterface
+swizzleClass:forClass: on ZKSwizzle
ZKSwizzle(ReplacementObject, OriginalObject);

ZKSwizzle also has macros in place for hooking instance variables:

// gets the value of _myIvar on self
int myIvar = ZKHookIvar(self, int, "_myIvar");
	
// gets the pointer to _myIvar on self so you can reassign it
int *myIvar = &ZKHookIvar(self, int, "_myIvar");
// set the value of myIvar on the object
*myIvar = 3;

You can also have grouped hooks, which means you can swizzle a specific class differently depending on something specific:

@interface GroupClass : NSObject
+ (NSString *)classMethod;
- (NSString *)instanceMethod;
@end

@implementation GroupClass
+ (NSString *)classMethod { return @"classMethod"; }
- (NSString *)instanceMethod { return @"instanceMethod"; }
@end

hook(GroupClass, Yosemite)
+ (NSString *)classMethod { return @"swizzled"; }
- (NSString *)instanceMethod { return @"swizzled"; }
endhook

hook(GroupClass, Mavericks)
+ (NSString *)classMethod { return @"swizzled2"; }
- (NSString *)instanceMethod { return @"swizzled2"; }
endhook

ctor {
    int ver = 1;
    ver == 1 ? ZKSwizzleGroup(Yosemite) : ZKSwizzleGroup(Mavericks);
}

"Swizzling the right way"

Some say that using method_exchangeImplementations causes problems with the original implementation being passed a replaced _cmd such as old_description which would be the new selector for the original implementation of a swizzled description. ZKSwizzle solves this problem with ZKOrig(TYPE, ...) which passes the correct selector to the original implementation and thus avoids this problem.

#License

ZKSwizzle is available on the permissive MIT License

More Repositories

1

Mousecape

Cursor Manager for OSX
Objective-C
1,206
star
2

optool

Command Line Tool for interacting with MachO binaries on OSX/iOS
Objective-C
1,120
star
3

ThemeEngine

OS X App to edit compiled .car files
Objective-C
736
star
4

ZKRevealingTableViewCell

A Sparrow-style Implementation of Swipe-To-Reveal
Objective-C
339
star
5

StartAtLoginController

A class that uses the new ServiceManagement api to allow apps to run at login.
Objective-C
206
star
6

Opee

Code Injection Platform for OS X
C
109
star
7

ScrollToRefresh

An NSScrollView subclass to allow the popular "pull to refresh" from iPhone to work on Mac OS X Lion
Objective-C
102
star
8

sartFileTool

command line tool to decode/encode SArtFile.bin
Objective-C
75
star
9

artFileTool

a tool to decode ArtFile.bin
Objective-C
72
star
10

ZKTextField

NSTextField replacement
Objective-C
72
star
11

Zeppelin

Carrier Logo Manager
Objective-C
38
star
12

Black-Mac-OS-X

A SIMBL plugin to turn 10.6.x Snow Leopard into a black tub of awesomeness.
Objective-C
33
star
13

TinyBar

iOS Tweak that thins notification banners down to size!
Objective-C
31
star
14

dockify

The application/installer used for Dockify
Objective-C
17
star
15

ZKInspector

Xcode/IB-like inspector class for OSX
Objective-C
14
star
16

FBEye

SIMBL plugin for various tweaks in the OS
Objective-C
14
star
17

ICOFamily

A class for reading and saving ICO files. Works with 64px and 24px sizes in addition to 256, 128, 48, 32, 16.
Objective-C
12
star
18

icnsmake

Command Line Tool for quickly making and exporting icns files
Objective-C
8
star
19

AutumnBoard

Icon Theming Library for 10.10
Objective-C
6
star
20

ILScopebar

A cocoa scope bar that uses an overflow menu.
Objective-C
5
star
21

dotfiles

Shell
4
star
22

repo

my cydia repo
Perl
3
star
23

Eclipse

A color theme for Xcode
1
star
24

cel-admission-polyfill

a polyfill for using CEL for admission control in kubernetes
Go
1
star
25

Grid

An online multiplayer implementation of Connect4 in javascript
JavaScript
1
star
26

eatery

FOOD!
Objective-C
1
star
27

alexzielenski.github.com

JavaScript
1
star
28

graph

1
star
29

dockify_library

The actual library used in Dockify
Objective-C
1
star
30

WWDC-2015

My 2015 WWDC Scholarship Application
Swift
1
star