Reputation: 5892
I have an Obj-C method that takes a boolean, and the name of the boolean so it can be logged:
- (void)shouldBeEnabled:(BOOL)flag flagName:(NSString *)flagName {
if (!flag) {
NSLog(@"Not enabled because %@ is false", flagName);
}
}
And I call it like this:
BOOL swizzlingEnabled = NO;
[self shouldBeEnabled:swizzlingEnabled flagName:@"swizzlingEnabled"];
While this works, it's really annoying to copy the parameter as a string, just so it can be logged, and leaves it open for the log to get out of sync with the actual parameter that is passed in when it gets renamed or something.
Ideally the method would drop the flagName
parameter, but it would still work in the same way, where it would print the literal string that is used. For example:
[self shouldBeEnabled:swizzlingEnabled];
// prints "Not enabled because swizzlingEnabled is false"
// And it would be extremely cool if this would also work:
[self shouldBeEnabled:(someOptionHere == nil)];
// prints "Not enabled because (someOptionHere == nil) is false"
Is this possible at all?
(Yes I realize these code examples look very silly.. why would you create the swizzlingEnabled
variable just to pass it into the function in the next line. Obviously in the real world app there is quite a bit more going on, but this is what it boils down to 😅)
Upvotes: 0
Views: 24