Skip to content

Instantly share code, notes, and snippets.

@quantumpotato
Created January 3, 2012 21:55
Show Gist options
  • Save quantumpotato/1557144 to your computer and use it in GitHub Desktop.
Save quantumpotato/1557144 to your computer and use it in GitHub Desktop.
NSNotification observing with blocks
#import <Foundation/Foundation.h>
@interface NSNotificationCenter (CHAdditions)
+ (void)addObserver:(id)observer forNotificationNamed:(NSString *)notificationName withBlock:(void (^)(NSNotification *note))block;
@end
#import "NSNotificationCenter+CHAdditions.h"
@implementation NSNotificationCenter (CHAdditions)
+ (void)addObserver:(id)observer forNotificationNamed:(NSString *)notificationName withBlock:(void (^)(NSNotification *note))block {
[[NSNotificationCenter defaultCenter] addObserverForName:notificationName
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:block];
}
@end
@quantumpotato
Copy link
Author

Considered a category on NSObject but felt that violated the purpose of NSNotificationCenter.
Slightly less messy, doesn't extend your notification handling code so far to the right in your editor.

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