Challenge: Using a block with NSNotificationCenter


#1

not much to dwell on here, but here is mine anyway… (I did end up specifying a queue as I was going by the framework reference at the time)

[code]int main(int argc, const char * argv[]) {
@autoreleasepool {

 //   ZGCLogger *logger = [[ZGCLogger alloc] init]; //object we are using for the callbacks
    
    
    /// ++ NOTIFICATION CENTER Using method that takes a BLOCK as an argument instead of a callback approach from before ++ //
    [[NSNotificationCenter defaultCenter] addObserverForName:NSSystemTimeZoneDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification *note){
                                                      
                                                      NSLog(@"Time zone has been changed to %@", [NSTimeZone systemTimeZone]);
                                                      
                                                  }];

                                                            
    NSLog(@"Current Time Zone is %@", [NSTimeZone systemTimeZone]);
    
    
    /* event driven programs use an object which sits listetning for events (NSRunLoop). it is always running until
     told to stop when an event happens, the run loop causes a call back to occur. keeps the thread running
     waiting for an event (button press, timer, etc.etc)
     */
    [[NSRunLoop currentRunLoop] run]; //
}
return 0;

}[/code]

[color=#0000FF]********** OUTPUT *****************
2014-12-11 09:56:37.750 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Current Time Zone is America/New_York (EST) offset -18000

2014-12-11 14:56:48.102 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Time zone has been changed to Europe/Dublin (GMT) offset 0
2014-12-11 19:56:50.725 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Time zone has been changed to Asia/Oral (GMT+5) offset 18000
2014-12-11 22:56:52.696 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Time zone has been changed to Asia/Chongqing (GMT+8) offset 28800
2014-12-11 16:56:54.915 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Time zone has been changed to Asia/Damascus (GMT+2) offset 7200
2014-12-11 09:57:19.175 ObjC_Blocks_NSNotificationCenter - Challenge[7521:1713471] Time zone has been changed to America/New_York (EST) offset -18000[/color]