views:

116

answers:

2

Something's wrong with my code here and I can't quite figure it out.

Edit: Please correct this code. Thanks!

int stringLength = [theData length];
for (int i = 1; i <= stringLength; i++) {
        unichar currentCharacter = [theData characterAtIndex:i];
        int currentCharacterCode = keyCodeForKeyString(currentCharacter);
        CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)currentCharacterCode, true);
        CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)currentCharacterCode, false);
}

- (int)keyCodeForKeyString:(unichar)keyString
{
    if (strcmp(keyString, "a") == 0) return 0;
    if (strcmp(keyString, "s") == 0) return 1;
    if (strcmp(keyString, "d") == 0) return 2;
    if (strcmp(keyString, "f") == 0) return 3;
    if (strcmp(keyString, "h") == 0) return 4;
}
+2  A: 

For one thing, characterAtIndex is zero-indexed. So, you're starting at the second character of theData, and (assuming theData and theString have the same length) reading one past the end (which is undefined behavior). For another, characterAtIndex returns unichar, not a pointer-to-char. You're also calling keyCodeForKeyString on the pointer, when you should be passing it as a parameter. But you could change keyCodeForKeyString to take a unichar, rather than a one-character null-terminated string.

Matthew Flaschen
ah thanks, i figured that [NSString length] started at 1, but i didn't realize that characterAtIndex started at 0. thanks.what is a unichar and what do i need to do?
Wind Up Toy
A `unichar` represents a Unicode code unit. Internally, it's an unsigned short. `[NSString length]` does *not* start at 1. It can be zero, which means the string is empty (and you can't call characterAtIndex). Also remember zero-indexing implies the last character is length - 1.
Matthew Flaschen
I hate to seem annoying, but could you please correct the code? I'm not getting anywhere. I would learn by seeing what is correct. Here's what I've got...`for (int i = 1; i <= stringLength; i++) { unichar currentCharacter = [theData characterAtIndex:i]; int currentCharacterCode = keyCodeForKeyString(currentCharacter); CGPostKeyboardEvent((CGCharCode)0, (CGKeyCode)currentCharacterCode, true); }- (int)keyCodeForKeyString:(unichar)keyString{if (strcmp(keyString, "a") == 0) return 0; if (strcmp(keyString, "s") == 0) return 1;......` not sure why it wont format for code...
Wind Up Toy
@Wind Up, please post your code as an edit to the question. Then, you can format it properly. You haven't fixed the loop indices yet. If you're using `unichar`, you can just compare directly to a character constant (e.g. 'a'). Note the single quotes. You don't use `strcmp` for that. See also http://stackoverflow.com/questions/1092695/objective-c-unichar-vs-char .
Matthew Flaschen
i appreciate the help!
Wind Up Toy
Wind Up Toy: A `unichar` is a single character, just as `char` is. A C string is a *pointer to an array of characters*, typically declared as either `char[]` (e.g., `char str[]`) or `char *` (`char *str`). `strcmp` only takes a C string, and does not know how to deal with C strings made of `unichar`s instead of `char`s. Since you have a single `unichar`, you should simply compare it using the equality operator; `strcmp` is only for C strings of `char`.
Peter Hosey
+2  A: 

This line:

int currentCharacterCode = keyCodeForKeyString(currentCharacter);

is a problem. You don't seem to have a function called keyCodeForKeyString. You do have a method with that name, but the syntax for that would be

int currentCharacterCode = [self keyCodeForKeyString: currentCharacter];
//                          ^^^^ I assume you are sending the message from the same object.
JeremyP