Created
January 21, 2014 11:07
-
-
Save tarunon/8538136 to your computer and use it in GitHub Desktop.
https://twitter.com/kain3939/status/425562408594051072 より 1+1を3にしてみた。
ただのMethodSwizzling。あまりエレガントではない。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import <objc/runtime.h> | |
| #import <objc/objc.h> | |
| #import <objc/message.h> | |
| @implementation NSDecimalNumber (hack) | |
| - (NSDecimalNumber *)decimalNumberByAdding_:(NSDecimalNumber *)decimalNumber | |
| { | |
| return [[self decimalNumberByAdding_:decimalNumber] decimalNumberByAdding_:(self.integerValue == decimalNumber.integerValue == 1) ? decimalNumber : [NSDecimalNumber zero]]; | |
| } | |
| @end | |
| void tarunon() | |
| { | |
| Class klass = [NSDecimalNumber class]; | |
| method_exchangeImplementations(class_getInstanceMethod(klass, @selector(decimalNumberByAdding:)), class_getInstanceMethod(klass, @selector(decimalNumberByAdding_:))); | |
| } | |
| int main(int argc, const char * argv[]) | |
| { | |
| @autoreleasepool { | |
| NSLog(@"1 + 1 = %@", [[NSDecimalNumber one] decimalNumberByAdding:[NSDecimalNumber one]]); | |
| tarunon(); | |
| NSLog(@"1 + 1 = %@", [[NSDecimalNumber one] decimalNumberByAdding:[NSDecimalNumber one]]); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment