A quick and "lean" way to swizzle methods for your Objective-C development needs.
CocoaPods is the recommended way to add Swizzlean
to your project.
- Add Swizzlean to your Podfile
pod 'Swizzlean'
. - Install the pod(s) by running
pod install
. - Add Swizzlean to your files with
#import <Swizzlean/Swizzlean.h>
.
Swift Package Manager can be used to add Swizzlean
the to your project:
- Add
.package(url: "https://github.com/rbaumbach/Swizzlean", from: "1.1.0")
- Follow intructions to add the
Swizzlean
package to your project.
You can also use Carthage to manually add the Swizzlean
framework to your project.
- Add
github "rbaumbach/Swizzlean"
to your Cartfile. - Follow instructions to manually add Swizzlean dynamic framework to your project.
- Clone repository from github and copy files directly, or add it as a git submodule.
- Add Swizzlean and RuntimeUtils (.h and .m) files to your project.
- Create an instance of Swizzlean passing in the class of the methods you want to swizzle.
- Call
swizzleInstanceMethod:withReplacementImplementation:
for an instance method passing in the selector of the method to be swizzled with the replacement implementation. When passing in the replacement implementation block, the first parameter is alwaysid _self
(pointer to the 'Class' being swizzled), and the followed by any other parameters for the method being swizzled. - Call
swizzleClassMethod:withReplacementImplementation:
for a class method passing in the selector of the method to be swizzled with the replacement implementation. - You can check the current instance/class method that is swizzled by using the
currentInstanceMethodSwizzled
andcurrentClassMethodSwizzled
methods. - The status of the swizzled methods can be seen by calling
isInstanceMethodSwizzled
andisClassMethodSwizzled
. - Use reset methods to unswizzle the instance/class methods that are currently being swizzled.
- Methods are automatically reset when the Swizzlean object is deallocated. If you would like to
keep the methods swizzled after
dealloc
is called, set the propertyresetWhenDeallocated = NO
.
Swizzlean *swizzle = [[Swizzlean alloc] initWithClassToSwizzle:[NSString class]];
[swizzle swizzleInstanceMethod:@selector(intValue) withReplacementImplementation:^(id _self) {
return 42;
}];
NSString *number7 = @"7";
NSLog(@"The int value for number7 is: %d", [number7 intValue]);
// returns - The int value for number7 is: 42
[swizzle resetSwizzledInstanceMethod];
NSLog(@"The int value for number7 is: %d", [number7 intValue]);
// returns - The int value for number7 is: 7
This project has been setup to use fastlane to run the specs.
First, run the setup.sh
script to bundle required gems and Cocoapods
when in the project directory:
$ ./setup.sh
And then use fastlane
to run all the specs on the command line:
$ bundle exec fastlane specs
Version history can be found on releases page.
Thanks for checking out Swizzlean
for your swizzling needs. Any feedback can be
can be sent to: github@ryan.codes.
Thanks to the following contributors for keeping Swizzlean
Swizzletastic:
Erik Stromlund & Aaron Koop