问题描述:

today i started with a simple Project that should convert a string into an array with keycodes to simulate keystrokes.

My Problem is that i cant use a char as an id for a dictionary to convert that char into the keycode.

//Declare a Dictionary

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

//Add the Basic data for keycodes (i know that there are some missing)

[dictionary setObject:@"0x00" forKey:@"a"]; //a

[dictionary setObject:@"0x0B" forKey:@"b"]; // b

[dictionary setObject:@"0x08" forKey:@"c"]; //c

[dictionary setObject:@"0x02" forKey:@"d"]; // d

[dictionary setObject:@"0x0E" forKey:@"e"]; //e

[dictionary setObject:@"0x03" forKey:@"f"]; //f

[dictionary setObject:@"0x05" forKey:@"g"]; //g

[dictionary setObject:@"0x04" forKey:@"h"]; //h

[dictionary setObject:@"0x22" forKey:@"i"]; //i

[dictionary setObject:@"0x26" forKey:@"j"]; //j

[dictionary setObject:@"0x28" forKey:@"k"]; //k

[dictionary setObject:@"0x25" forKey:@"l"];//l

[dictionary setObject:@"0x2D" forKey:@"m"];//m

[dictionary setObject:@"0x2E" forKey:@"n"];//n

[dictionary setObject:@"0x1F" forKey:@"o"];//o

[dictionary setObject:@"0x23" forKey:@"p"];//p

[dictionary setObject:@"0x0C" forKey:@"q"];//q

[dictionary setObject:@"0x0F" forKey:@"r"];//r

[dictionary setObject:@"0x01" forKey:@"s"];//s

[dictionary setObject:@"0x11" forKey:@"t"];//t

[dictionary setObject:@"0x20" forKey:@"u"];//u

[dictionary setObject:@"0x09" forKey:@"v"];//v

[dictionary setObject:@"0x0D" forKey:@"w"];//w

[dictionary setObject:@"0x07" forKey:@"x"];//x

[dictionary setObject:@"0x10" forKey:@"y"];//y

[dictionary setObject:@"0x06" forKey:@"z"];//z

NSString *workwith = [_mess stringValue]; //Get String from Interface

long stringl = [workwith length]; //Get lenght of the String

int a = 0;

char text[stringl-1]; //make a array with the lenght of the string

while (a < stringl) { //fill this array with chars from the string

text[a] = [workwith characterAtIndex:a];

NSLog(@"%c",text[a]);

a++;

}

char fmat[stringl-1]; //make a second char to fill it with keycodes

int dnehmen = 0;

while (dnehmen <= stringl-1) {

fmat[dnehmen] = [dictionary objectForKey:@"%c",text[dnehmen]]; //stuck at this point

}

At the last line i try to use a char from the first array as an id for the dictionary to get the keycode for the second array.

I get an error that i cant use a char as an id and so i need a was to get the keycode from a char.

(I read some Posts on this site but i didn`t really understood what they´ve done)

Thanks for your answers

Tim

网友答案:

The error you report is down to the line:

fmat[dnehmen] = [dictionary objectForKey:@"%c",text[dnehmen]];

as @"%c",text[dnehmen] does not return an object but a char value due to the use of the rather obscure C comma operator - a comma is usually a separator but is also an operator (like + etc.) which just returns its second argument. So you get an error referring to characters...

Given that you've used NSString values as keys what you need is a string containing your character and you probably meant to type:

fmat[dnehmen] = [dictionary objectForKey:[NSString stringWithFormat:@"%c",text[dnehmen]]];

While that will fix your first problem you now have another, your dictionary returns an NSString containing four characters and your code suggests you'd like the byte (char) represented by those four characters when interpreted by a compiler as a hexadecimal literal...

At this point you might be tempted to try to convert the string into a byte value by parsing it as a hexadecimal number, but you shouldn't. Instead look at putting the value you need into the NSDictionary in the first place. Both the objects any keys used in an NSDictionary can be any [1] type of object. You might want to consider using NSNumber objects for both.

Hopefully that will set you off on the right track in fixing your design.

[1] Well almost any to be precise, any which have equality and hash methods - which in practice is everything that descends from NSObject so don't worry about this pedantic detail now!

相关阅读:
Top