Created
May 30, 2013 08:17
Leveraging the preprocessor to lessen boilerplate. L(X) is the crux. It allows the me to apply different transformations over the same list.
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
#define S(_s) @selector(application##_s:) // selector generator | |
#define N(_n) UIApplication##_n##Notification // name generator | |
#define L(X) X(WillResignActive), X(DidEnterBackground), \ | |
X(WillEnterForeground), X(DidBecomeActive), X(WillTerminate) // apply X over list | |
#define C(_a) sizeof((_a))/sizeof((_a)[0]) // array count | |
- (void)monitorAppState:(BOOL)start | |
{ | |
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; | |
SEL s[] = { L(S) }; | |
NSString* n[] = { L(N) }; | |
NSAssert(C(n) == C(s), @"Name and selector arrays have same size."); | |
for(NSUInteger i = 0; i < C(n); ++i) | |
if([self respondsToSelector:s[i]]) | |
{ | |
if(start) | |
[nc addObserver:self selector:s[i] name:n[i] object:nil]; | |
else | |
[nc removeObserver:self name:n[i] object:nil]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment