Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Helper to call target/action from an UIBarButtonItem.
#define PSCCAssert(condition, description, ...) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wnullable-to-nonnull-conversion\"") \
NSCAssert(condition, description, ##__VA_ARGS__) \
_Pragma("clang diagnostic pop")
#define PSCWeakifyAs(object, weakName) typeof(object) __weak weakName = object
void (^psc_targetActionBlock(id target, SEL action))(id) {
// If there's no target, return an empty block.
if (!target) return ^(__unused id sender) {};
// All ObjC methods have two arguments. This fails if either target is nil, action not implemented or else.
NSUInteger numberOfArguments = [target methodSignatureForSelector:action].numberOfArguments;
PSCCAssert(numberOfArguments == 2 || numberOfArguments == 3, @"%@ should have at most one argument.", NSStringFromSelector(action));
PSCWeakifyAs(target, weakTarget);
if (numberOfArguments == 2) {
return ^(__unused id sender) { PSC_SILENCE_CALL_TO_UNKNOWN_SELECTOR([weakTarget performSelector:action];) };
} else {
return ^(id sender) { PSC_SILENCE_CALL_TO_UNKNOWN_SELECTOR([weakTarget performSelector:action withObject:sender];) };
// Usage:
// psc_targetActionBlock(, pdfController.outlineButtonItem.action)(nil);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment