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.