Skip to content

Instantly share code, notes, and snippets.

@eytanbiala
Forked from rudyjahchan/NSObject_Swizzle.h
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save eytanbiala/5bf68aef5ed1f14e4750 to your computer and use it in GitHub Desktop.

Select an option

Save eytanbiala/5bf68aef5ed1f14e4750 to your computer and use it in GitHub Desktop.

Revisions

  1. Rudy Jahchan revised this gist Feb 20, 2013. 1 changed file with 24 additions and 17 deletions.
    41 changes: 24 additions & 17 deletions SwizzleUIVIewController.m
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,25 @@
    SEL viewWillAppearSelector = @selector(viewWillAppear:);
    SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:);
    Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector);
    Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector);

    BOOL methodAdded = class_addMethod([self class],
    viewWillAppearSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    tourGuideWillAppearSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    @implementation UIViewController (TourGuide)

    + (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    SEL viewWillAppearSelector = @selector(viewWillAppear:);
    SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:);
    Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector);
    Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector);

    BOOL methodAdded = class_addMethod([self class],
    viewWillAppearSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    tourGuideWillAppearSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    }
    });
    }
  2. Rudy Jahchan revised this gist Feb 20, 2013. 2 changed files with 10 additions and 8 deletions.
    8 changes: 4 additions & 4 deletions SwizzleExample.m
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    SEL firstMethodSelector = @selector(firstMethod)
    SEL secondMethodSelector = @selector(secondMethod)
    SEL firstMethodSelector = @selector(firstMethod);
    SEL secondMethodSelector = @selector(secondMethod);
    Method firstMethod = class_getInstanceMethod(self, firstMethodSelector);
    Method secondMethod = class_getInstanceMethod(self, secondMethodSelector);

    BOOL methodAdded = class_addMethod([self class],
    firstMethod,
    firstMethodSelector,
    method_getImplementation(secondMethod),
    method_getTypeEncoding(secondMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    secondMethod,
    secondMethodSelector,
    method_getImplementation(firstMethod),
    method_getTypeEncoding(firstMethod));
    } else {
    10 changes: 6 additions & 4 deletions SwizzleUIVIewController.m
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,16 @@
    Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:));
    SEL viewWillAppearSelector = @selector(viewWillAppear:);
    SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:);
    Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector);
    Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector);

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
    viewWillAppearSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    newSelector,
    tourGuideWillAppearSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
  3. Rudy Jahchan revised this gist Feb 20, 2013. 2 changed files with 27 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions SwizzleExample.m
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,18 @@
    Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:));
    SEL firstMethodSelector = @selector(firstMethod)
    SEL secondMethodSelector = @selector(secondMethod)
    Method firstMethod = class_getInstanceMethod(self, firstMethodSelector);
    Method secondMethod = class_getInstanceMethod(self, secondMethodSelector);

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));
    firstMethod,
    method_getImplementation(secondMethod),
    method_getTypeEncoding(secondMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    newSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    secondMethod,
    method_getImplementation(firstMethod),
    method_getTypeEncoding(firstMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    method_exchangeImplementations(firstMethod, secondMethod);
    }
    16 changes: 16 additions & 0 deletions SwizzleUIVIewController.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:));

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    newSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    }
  4. Rudy Jahchan revised this gist Feb 20, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions SwizzleExample.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);
    Method originalMethod = class_getInstanceMethod(self, @selector(viewWillAppear:));
    Method newMethod = class_getInstanceMethod(self, @selector(tourGuideWillAppear:));

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
  5. Rudy Jahchan revised this gist Feb 20, 2013. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions UIViewController_TourGuide_Override.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #import "UIViewController_TourGuide.h"

    @implementation UIViewController (TourGuide)

    //...

    - (void) tourGuideWillAppear:(BOOL)animated {
    // call the old implementation, now under the new message signature
    [self tourGuideWillAppear:animated];

    if (![self isTourRan]) {
    [self displayTour];
    }
    }

    //...

    @end
  6. Rudy Jahchan revised this gist Feb 20, 2013. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions SwizzleExample.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    newSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    }
  7. Rudy Jahchan created this gist Mar 25, 2012.
    8 changes: 8 additions & 0 deletions NSObject_Swizzle.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #import <Foundation/Foundation.h>

    @interface NSObject (Swizzle)

    + (void)swizzleInstanceSelector:(SEL)originalSelector
    withNewSelector:(SEL)newSelector;

    @end
    27 changes: 27 additions & 0 deletions NSObject_Swizzle.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #import "NSObject+Swizzle.h"
    #import <objc/runtime.h>

    @implementation NSObject (Swizzle)

    + (void) swizzleInstanceSelector:(SEL)originalSelector
    withNewSelector:(SEL)newSelector
    {
    Method originalMethod = class_getInstanceMethod(self, originalSelector);
    Method newMethod = class_getInstanceMethod(self, newSelector);

    BOOL methodAdded = class_addMethod([self class],
    originalSelector,
    method_getImplementation(newMethod),
    method_getTypeEncoding(newMethod));

    if (methodAdded) {
    class_replaceMethod([self class],
    newSelector,
    method_getImplementation(originalMethod),
    method_getTypeEncoding(originalMethod));
    } else {
    method_exchangeImplementations(originalMethod, newMethod);
    }
    }

    @end
    7 changes: 7 additions & 0 deletions UIViewController_TourGuide.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #import <UIKit/UIKit.h>

    @interface UIViewController (TourGuide)

    @property (nonatomic, retain) NSArray* tourSteps;

    @end
    42 changes: 42 additions & 0 deletions UIViewController_TourGuide.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #import "UIViewController_TourGuide.h"
    #import "NSObject_Swizzle.h"

    static char tourStepsKey;

    @implementation UIViewController (TourGuide)

    + (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    [self swizzleInstanceSelector:@selector(viewWillAppear:)
    withNewSelector:@selector(tourGuideWillAppear:)];
    });
    }

    - (NSArray*) getTourSteps {
    return objc_getAssociatedObject(self, &tourStepsKey);
    }

    - (void) setTourSteps:(NSArray*)tourSteps {
    objc_setAssociatedObject(self, &tourStepsKey,
    tourSteps, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

    - (void) displayTour {
    // run the tour, and save the fact it was run
    }

    - (BOOL) isTourRan {
    // return TRUE if tour has been run
    }

    - (void) tourGuideWillAppear:(BOOL)animated {
    // call the old implementation, now under the new message signature
    [self tourGuideWillAppear:animated];

    if (![self isTourRan]) {
    [self displayTour];
    }
    }

    @end