Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
- Subject: Re: NSEvent locationInWindow undefined for non-mouse events, how to catch?
- From: Martin Wierschin <email@hidden>
- Date: Tue, 14 Feb 2012 14:54:29 -0800
> I've just been bitten by this. Seems it would be easy to detect at runtime and assert, is there any magic environment variable or defaults value that can help me catch such incorrect usage? I've searched but not found...
I don't know if there's any runtime/debug value, but this is one of those times where Objective-C's dynamic nature lets you add what you need. Just swizzle the implementation for -[NSEvent locationInWindow] for debug/development builds, eg:
#ifdef DEBUG
static NSPoint (*_XXEventLocationInWindowReal)( NSEvent* self, SEL _cmd ) = NULL;
static NSPoint _XXEventLocationInWindowOverride( NSEvent* self, SEL _cmd )
{
NSAssert( [self isMouseEventType], @"Must not ask non-mouse NSEvent's for locationInWindow" );
return _XXEventLocationInWindowReal( self, _cmd );
}
+ (void) initialize
{
if( NULL == _XXEventLocationInWindowReal ) {
Method meth = class_getInstanceMethod( self, @selector(locationInWindow) );
NSAssert( NULL != meth, @"NSEvent method should exist" );
_XXEventLocationInWindowReal = (void*)method_setImplementation(meth, (IMP)_XXEventLocationInWindowOverride);
NSAssert( NULL != _XXEventLocationInWindowReal, @"could not swizzle NSEvent method" );
}
}
#endif
I'd recommend hiding some of that ugly swizzling code with nice categories on NSObject.
~Martin
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden