My Notification Won't Notify


#1

I was going along fine with this chapter – until I hit the Notification section. I followed the instructions, and have checked my code several times to make sure I’ve got the NSNotificationCenter part correct, but whenever I run it, I get this error message:

2012-02-18 17:41:18.104 Callbacks[34555:707] Ouch! 2012-02-18 17:41:20.104 Callbacks[34555:707] Ouch! 2012-02-18 23:41:20.983 Callbacks[34555:707] -[Logger zoneChange]: unrecognized selector sent to instance 0x100114190 2012-02-18 23:41:20.985 Callbacks[34555:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Logger zoneChange]: unrecognized selector sent to instance 0x100114190' *** First throw call stack: ( 0 CoreFoundation 0x00007fff8a697fc6 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff966d7d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff8a7242ae -[NSObject doesNotRecognizeSelector:] + 190 3 CoreFoundation 0x00007fff8a684e73 ___forwarding___ + 371 4 CoreFoundation 0x00007fff8a684c88 _CF_forwarding_prep_0 + 232 5 Foundation 0x00007fff92aa2d32 __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 + 47 6 CoreFoundation 0x00007fff8a640aaa _CFXNotificationPost + 2634 7 CoreFoundation 0x00007fff8a6eac46 __CFNotificationCenterGetLocalCenter_block_invoke_3 + 342 8 libsystem_notify.dylib 0x00007fff8dbca7b3 ___notify_dispatch_handle_block_invoke_1 + 23 9 libdispatch.dylib 0x00007fff8ad158ba _dispatch_call_block_and_release + 18 10 libdispatch.dylib 0x00007fff8ad1772a _dispatch_main_queue_callback_4CF + 308 11 CoreFoundation 0x00007fff8a62d06c __CFRunLoopRun + 1724 12 CoreFoundation 0x00007fff8a62c676 CFRunLoopRunSpecific + 230 13 Foundation 0x00007fff92a97f9f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 267 14 Foundation 0x00007fff92a97e8b -[NSRunLoop(NSRunLoop) run] + 62 15 Callbacks 0x0000000100001721 main + 513 16 Callbacks 0x0000000100001514 start + 52 17 ??? 0x0000000000000001 0x0 + 1 ) terminate called throwing an exceptionsharedlibrary apply-load-rules all Current language: auto; currently objective-c (gdb)

Has anyone else seen anything like this? I’ll include my own code, in case I’ve not checked it carefully enough. :wink:

[code]//
// main.m
// Callbacks
//
// Created by Yvonne Truhon on 2/17/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “Logger.h”

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

@autoreleasepool {
	Logger *logger = [[Logger alloc] init];
	
	[[NSNotificationCenter defaultCenter] addObserver:logger selector:@selector(zoneChange) name:NSSystemTimeZoneDidChangeNotification object:nil];
	
	NSURL *url = [NSURL URLWithString:
				  @"http://www.gutenberg.org/cache/epub/205/pg205.txt"];
	NSURLRequest *request = [NSURLRequest requestWithURL:url];
	
	__unused NSURLConnection *fetchConn = [[NSURLConnection alloc] initWithRequest:request delegate:logger
																  startImmediately:YES];
	
__unused	NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:logger selector:@selector(sayOuch:) userInfo:nil repeats:YES];
    
	[[NSRunLoop currentRunLoop] run];

    
}
return 0;

}

[/code]

BTW, before I changed the time zone setting, everything was going along perfectly.

[code]//
// Logger.m
// Callbacks
//
// Created by Yvonne Truhon on 2/17/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import “Logger.h”

@implementation Logger

  • (void)sayOuch:(NSTimer *)t;
    {

    NSLog(@“Ouch!”);
    }
    // Called each time a chunk of data arrives

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    NSLog(@“Received %lu bytes”, [data length]);

// Create a mutable data object if it doesn’t already exist
if (!incomingData) {
incomingData = [[NSMutableData alloc] init];
}

[incomingData appendData:data];
}

  • (void)zoneChange:(NSNotification *)note
    {
    NSLog(@“The system time zone has changed!”);
    }

// Called when the last chunk has bneen processed

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    NSLog(@“Got it all!”);
    NSString *string = [[NSString alloc] initWithData:incomingData encoding:NSUTF8StringEncoding];
    incomingData = nil;

    NSLog(@“string has %lu characters”, [string length]);
    //Uncomment the next line to see the entire fetched file

    // NSLog(@“the whole string is %@”, string);
    }
    // Called if the fetch fails

  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    NSLog(@“connection failed: %@”, [error localizedDescription]);
    incomingData = nil;
    }

@end
[/code]

[code]//
// Logger.h
// Callbacks
//
// Created by Yvonne Truhon on 2/17/12.
// Copyright © 2012 MyCompanyName. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Logger : NSObject {
NSMutableData *incomingData;
}

  • (void)sayOuch:(NSTimer *)t;
  • (void)zoneChange:(NSNotification *)note;

@end
[/code]

I’ve tried changing where I placed the notification method in Logger.m, but it didn’t seem to help. Any clues would be much appreciated!

(And no big rush to send those clues – I’ve got to head off to the community theatre where I volunteer as a follow spot operator. Maybe the answer will sneak up on me during one of the songs!)

Love this forum, love the book.


#2

I was having a problem and checked out the forum and noted that I had the same problem as you, so I went back and took a closer look at the code. It took some time to finally find my mistake. In Main.m you forgot the “:” after zoneChange.

[[NSNotificationCenter defaultCenter] addObserver:logger selector:@selector(zoneChange:) name:NSSystemTimeZoneDidChangeNotification object:nil];

Doug


#3

Thanks, Doug!

I corrected my code before I left for today’s matinee, and then I had fun switching time zones to all the places I’d ever visited and watching for the “Timezone changed” alert.

Interesting item: sometimes the code will cause my computer to display the “Timezone changed” message once, and sometimes twice, as was mentioned in the book – with no pattern I could discern. I am curious to know why (like the inspiration for my “handle”), but right now I am just happy to get my code correct for this chapter, and grateful for your help.

Have a wonderful week!

ComputerCat12