orientationChanged: used without being declaration in .h?


Can someone please guide me on this?

Chapter 9: Notification and Rotation (2nd edition page 161)

how is the the method [color=#008040]orientationChanged:[/color] being used without being declared in the .h file?

[color=#008040]orientationChanged:[/color] is implemented in [color=#008000]HeavyRotationAppDelegate.m[/color] as seen below.
[color=#804000]-(void)orientationChanged:(NSNotification *)note
// log the constant that represents the current orientation
NSLog(@“orientation changed: %d”, [[note object] orientation]);

and invoked in the same class file.
[[color=#004040]nc addObserver:self

Earlier I tried writing a method in Hypnosis.m without declaring it in .h file. I tried calling that method (in the same class file) and got nasty errors. Errors went away only after I declared the method in the .h.

my method looked like this
[color=#FF8000]-(float)checkaxis:(float)axisvalue {
// do something

and called as [color=#FF8000] r = [self checkaxis];[/color] // self is the HypnosisView.m



Short answer:

The @selector() compiler directive is the secret sauce here. Methods accessed directly via their selectors and methods declared in protocols are the two big exceptions to the “declare it in your header” rule.

Long answer:

In Objective-C, you can send any message to any object. The compiler will throw a hissy when you explicitly send a message to an object if the message was not declared in the object’s class’ header file, such as:

NSString *aString;
[aString danceAndSing]; //compiler error

But if you tell the Objective-C runtime to directly invoke a method via its selector, then the compiler won’t complain. If the target object implements the method you’re trying to invoke, everything works. If the target object doesn’t implement the method, though, your application crashes with an Unrecognized Selector exception.

NSString *aString;
[aString performSelector:@selector(danceAndSing)]; // compiles fine, crashes when run

The NSNotificationCenter et al. use the performSelector: family of methods when invoking the selector you pass to them.


Thank you, that makes sense.
As you can tell I’m new to objective C.

So even though I can implement a method im not going to expose outside the class,
I dont have to declare it in the .h interface file but I still cannot use it within the class,
unless I prefix it with the @selector.