Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing up/down arrow behavior for NSTextField




On Feb 14, 2008, at 9:53 AM, glenn andreas wrote:


On Feb 14, 2008, at 8:44 AM, Duncan Champney wrote:
Happily, the keystrokes I want to special-case, up/down arrow and shift up/down arrow, have existing command selectors ("moveUp", "moveDown", "moveUpAndModifySelection", and "moveDownAndModifySelection"). I wrote my code to handle those 4 messages, and ignore others. Rather than using "- tryToPerform:aSelector", I just do a string match on the different command selectors, like this:

 NSLog(@"Command = %s", command);
 if (!strcmp((char *)command, "moveUp:"))
     //handle up arrow


This is very wrong - it just happens to work for you (due to the way it just happens to be implemented internally)

command is a selector (SEL) which is _not_ a (char *), and you shouldn't compare selectors using strcmp.

The solution is actually much cleaner and simpler:

  NSLog(@"Command = %@", NSStringFromSelector(command));
  if (command == @selector(moveUp:)) {
      // handle up arrow
  ....



Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next generation of fractal art






Glenn,

I gather my code works because a selector happens to contain the method name at the beginning, but I should not assume that? I certainly don't want to write my code based on internal structures that may not work in future releases. Thanks for the warning, and the safer way of doing it.


Duncan _______________________________________________


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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Changing up/down arrow behavior for NSTextField (From: Duncan Champney <email@hidden>)
 >Re: Changing up/down arrow behavior for NSTextField (From: glenn andreas <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.