Created
October 27, 2013 10:25
-
-
Save ElectricCoffee/7180084 to your computer and use it in GitHub Desktop.
From one of my projects, I'm keeping this way of doing it for future reference, so I won't forget; the project currently uses function pointers instead; but this could come in handy.
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
double invokeCalculator(id object, SEL method, double price, double extraItemPrice, int index) { | |
double returnValue = 0xDEAD; | |
if([object respondsToSelector:method]) { | |
NSLog(@"object responded sucessfully"); | |
NSInvocation* invocation = | |
[NSInvocation invocationWithMethodSignature: [object methodSignatureForSelector: method]]; | |
[invocation setTarget: object]; // index 0 | |
[invocation setSelector: method]; // index 1 | |
[invocation setArgument: &price atIndex: 2]; // index 2 | |
[invocation setArgument: &extraItemPrice atIndex: 3]; // and so on | |
[invocation setArgument: &index atIndex: 4]; | |
[invocation invoke]; | |
[invocation getReturnValue: &returnValue]; | |
} | |
return returnValue; // if it's 0xDEAD I know it's an error | |
} | |
// inside the class definition: | |
// MARK: Private Members | |
//TODO: finish the actual file-writing part of the method | |
-(void) writeToFileWithSelector: (SEL) selector price: (double) price andModifier: (double) extraItems { | |
double currentValue; | |
for (int i = 1; i < 10; i++) { | |
currentValue = invokeCalculator(self, selector, price, extraItems, i); | |
NSLog(@"%i current value: %f", i, currentValue); | |
} | |
} | |
-(double)calculatePercentWithPrice: (double)price andModifier: (double) modifier andIndex: (int) count { | |
return ((count + 1) * price) - ((count + 1) * price) * (modifier / 100); | |
} | |
-(double)calculatePriceWithPrice: (double)price andModifier: (double) modifier andIndex: (int) count { | |
return price + modifier * count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment