Created
November 8, 2012 20:10
-
-
Save justin/4041225 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice optimization.