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: Dix Lorenz <email@hidden>
- Date: Sat, 22 Mar 2003 12:09:57 +0100
Hi Jeff,
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.
You pretty much said it yourself: HotKeyHandler is a C-routine. It
doesn't matter that it is inside an @implementation-block, it is not a
method of your class. Would you expect it to know about "self" outside
of this block? So there :-)
What must I to do to get "self" to work in HotKeyHandler()?
Not knowing what HotKeyHandler really is: If it is a callback routine,
they usually have a field where the user can stick some data, this
would be the place to put a pointer to your object. If you just need
the running NSApplication, use the global variable NSApp or
[NSApplication sharedApplication]. In other cases your solution is
probably ok, but that really depends on if you have more then one
instance of the object. Then you need to have some way of determining
which object to use and that could get messy or even impossible.
Greetings,
Dix
_______________________________________________
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.