Skip to content

Instantly share code, notes, and snippets.

@justin
Created November 8, 2012 20:10
Show Gist options
  • Save justin/4041225 to your computer and use it in GitHub Desktop.
Save justin/4041225 to your computer and use it in GitHub Desktop.
//
// OLD AND BUSTED
//
if ([self.zoomingDelegate respondsToSelector:@selector(zoomingWindow:didZoomOutViewController:)] == YES)
{
// Do something important.
}
//
// NEW HOTNESS
//
// 1. A private struct with an option for each method you want to check against.
struct {
unsigned willZoomInViewController : 1;
unsigned didZoomInViewController : 1;
unsigned willZoomOutViewController : 1;
unsigned didZoomOutViewController : 1;
} _delegateHas;
// 2. Set those values in the delegate's setter.
- (void)setZoomingDelegate:(id<SGZoomingWindowDelegate>)zoomingDelegate
{
_zoomingDelegate = zoomingDelegate;
_delegateHas.willZoomInViewController = [_zoomingDelegate respondsToSelector:@selector(zoomingWindow:willZoomInViewController:)];
_delegateHas.didZoomInViewController = [_zoomingDelegate respondsToSelector:@selector(zoomingWindow:didZoomInViewController:)];
_delegateHas.willZoomOutViewController = [_zoomingDelegate respondsToSelector:@selector(zoomingWindow:willZoomOutViewController:)];
_delegateHas.didZoomOutViewController = [_zoomingDelegate respondsToSelector:@selector(zoomingWindow:didZoomOutViewController:)];
}
// 3. Call this shorter, easier to read snippet of code in-line
if (_delegateHas.didZoomOutViewController)
{
// Do stuff
}
@mike3k
Copy link

mike3k commented Nov 8, 2012

Nice optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment