Okay, not sure if this can be done but, what I want to do is... say that I have 3 int variables named a, b, and c... Now say that I have an NSString containing some letters, and have a loop run through the string letter by letter, and each time the letter 'a' is found, it would increment the variable a by 1, and the same for b and c. But something like this would require an if tree like this:
NSString *myString = @"abjbcaubuaedhoca"; // some random letters
NSString *thisLetter;
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < [myString length]; i++) {
thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // gets each letter.
if ([thisLetter isEqualToString"a"])
a++;
else if ([thisLetter isEqualToString"b"])
b++;
else if ([thisLetter isEqualToString"c"])
c++;
}
But what I want to do is remove the if tree... Something like this is what I want:
NSString *myString = @"abjbcaubuaedhoca"; // some random letters
NSString *thisLetter;
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < [myString length]; i++) {
thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // get each letter.
//something here... considering the contents of "thisLetter" is the name of the integer I want to increment, I was hoping that there would be some way to refer to a variable with a variable, as in "thisLetter" takes the place of the variable name, if that makes sense (i.e if the contents of "thisLetter" were "a" then, thisLetter++ would increment the value of "a" by 1).
}
If this can't be done, then I would also like to know if there is anything faster than an if tree... I was told the switch() can't be used on NSStrings.
NSString *myString = @"abjbcaubuaedhoca"; // some random letters
NSString *thisLetter;
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < [myString length]; i++) {
thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // gets each letter.
if ([thisLetter isEqualToString"a"])
a++;
else if ([thisLetter isEqualToString"b"])
b++;
else if ([thisLetter isEqualToString"c"])
c++;
}
But what I want to do is remove the if tree... Something like this is what I want:
NSString *myString = @"abjbcaubuaedhoca"; // some random letters
NSString *thisLetter;
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < [myString length]; i++) {
thisLetter = [this_word substringWithRange:NSMakeRange(i, 1)]; // get each letter.
//something here... considering the contents of "thisLetter" is the name of the integer I want to increment, I was hoping that there would be some way to refer to a variable with a variable, as in "thisLetter" takes the place of the variable name, if that makes sense (i.e if the contents of "thisLetter" were "a" then, thisLetter++ would increment the value of "a" by 1).
}
If this can't be done, then I would also like to know if there is anything faster than an if tree... I was told the switch() can't be used on NSStrings.