In order to better understand the startup, event queue, and methods within my application I’m trying to write a program that does two things: Play a beep at the startup and every time the user hits a button. So far it only plays when the user hits the button. I know there may be multiple ways to get the startup beep to play, but in order to work with initialization code I want to do it by calling my beep method from within the applicationDidFinishLaunching method of the AppDelegate.m file.
Here is my code:
Log.h
#import <Cocoa/Cocoa.h>
@interface Log : NSObject {
IBOutlet id button;
}
-(void)beepAndLog;
-(IBAction)buttonPressed:(id)sender;
@end
Log.m
#import "Log.h"
@implementation Log
-(void)beepAndLog {
NSLog(@"The Method Was Called!");
NSBeep();
}
-(IBAction)buttonPressed:(id)sender {
[self beepAndLog];
}
@end
And the applicationDidFinishLaunching method looks like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[Log beepAndLog];
}
In the applicationDidFinishLaunching method, however, XCode warns me that
‘Log’ may not respond to ‘+beepAndLog’
and indeed, there is no beep and the log reads as follows:
MethodResponse[11401:a0f] +[Log
beepAndLog]: unrecognized selector
sent to class 0x100002100
(“MethodResponse” is the name of my project, btw)
I’m unsure why Log wouldn’t respond to beepAndLog, seeing as that’s one of its methods. Am I calling it incorrectly? I have a feeling this will be painfully obvious to you more experienced people. I’m a newbie. Any help would be appreciated! Thanks!
There are two possibilities. Either you defined
beepAndLogas an instance method, when you wanted a class method, or you want to call it on an instance when you called it on the class.To change it to a class method, change the header to read:
and the implementation:
For the other solution, make sure you have an instance of class
Logaround (probably a singleton), and do something like:from your notification method. The
Logclass would need to look something like this:Log.h:
Log.m: