Re: Trouble with "self" in Carbon and Cocoa
Re: Trouble with "self" in Carbon and Cocoa
- Subject: Re: Trouble with "self" in Carbon and Cocoa
- From: Tobias Hermann <email@hidden>
- Date: Sat, 22 Mar 2003 11:50:05 +0100
Hi!
self within an instance of an object is a pointer to the object
instance itself.
That pointer self exists only within classes/objects. If you are
programming a simple flat C function (like you did), you are not within
any class or such. So there is no such thing as self.
As far as i know you cannot declare simple C functions as methods
within a class in objc. So what you could do is pass the object to the
hotkey handler as an argument and call a method of that passed object,
or as you did with the workaround with a global variable.
What would be the best depends on what you are doing... If there will
be only ONE instance of the object of your class, you could use the
Singleton design pattern. You could make a class method in your class
that gives back that one instance, e.g.
... within the class MyClass:
+ (MyClass *) getSingletonInstance; // if there is no instance yet,
create one, otherwise pass the existing one
within your hotkey-handler:
...
[[MyClass getSingletonInstance] myCocoaMethod: someparameter];
...
I hope I understood your problem at all :-)
best regards.
Tobi
On Saturday, March 22, 2003, at 11:23 AM, Jeffrey Mattox wrote:
I have a hotkey handler that needs to call a method using "self" but
it causes a compile error. Elsewhere in my program I'm using "self"
without error. The hotkey handler is a C routine because that's what
I need in order to register it as the handler. Here's the outline:
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
@implementation NSApplication (mainline)
OSStatus HotKeyHandler(...)
{
[anObject aMethod]; // works for Foundation, et al
[self myCocoaMethod:YES]; // <--- causes "self" undeclared error
{
-(void) myCocoaMethod:(BOOL)flag
{
// never get here
}
@end
As a workaround, I can make it work by saving the value of "self" in
another variable during initialization and use that later in my
handler. E.g,:
NSApplication *mySelf;
...
{
...
mySelf = self;
}
I want to understand why the compiler is complaining. What must I to
do to get "self" to work in HotKeyHandler()?
Jeff
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.