0

I've been trying to find a better way to switch on each character of a string.

My existing code is:

NSUInteger len = [oldName length], i;
SEL xSelector = @selector(characterAtIndex:);
unichar (*charAtIdx)(id, SEL, NSUInteger) = (typeof (charAtIdx)) [oldName methodForSelector:xSelector];
NSMutableString *NewName = [NSMutableString new];

for (i=0 ; i<len ; i++){
    unichar c = charAtIdx(oldName,xSelector,i);
    if (c == "Ú" || c == "°"){
         [NewName appendString:@"s"];
    }

    else if (c == "Û" || c == "”"){
         [NewName appendString:@"s"];
    }

    else if (c == "◊" || c == "˜"){
         [NewName appendString:@"x"];
    }

   else blablabla
}
return NewName;

Now, the above seems to be working, however i have about 50 if statements that "switch" mainly extended ASCII codes (character codes 128-255) to more meaningful ones.

I thought about using a switch statement with a typedef enum and switch on that, however, the below doesn't work:

typedef enum {·,¡,Ê,∆} ExtendedASCII;

The idea would be to replace "unichar c = charAtIdx(oldName,xSelector,i);" with the below:

ExtendedASCII c = charAtIdx(oldName,xSelector,i);
Switch c
    case 0: //being ·
    case 1: // being ¡
    blablabla

Any ideas????

thanks, alex

alex
  • 17
  • 4
  • The usual way is to add such data to a dictionary. This can usually be searched very easily. Use, for instance, `[NSDictionary dictionaryWithObjectsandKeys: ... , nil];` to set it up and `objectForKey:` to search it. Turn each character into a key (NSString) and each replacement into a string. – Rudy Velthuis Feb 03 '14 at 14:37
  • `if (c == "Ú")` *cannot* work correctly because you compare an `unichar` with a string. And why do you use `methodForSelector` instead of simply calling `[oldName characterAtIndex:i]`? - Finally, why do your strings contain the "wrong" characters at all? Perhaps you have an encoding problem that can be solved at an earlier step, when the `oldName` string is created. – Martin R Feb 03 '14 at 18:59
  • I'm using the methodForSelector because reading the below it seems to be faster http://stackoverflow.com/questions/10198913/enumerate-nsstring-characters-via-pointer – alex Feb 03 '14 at 19:04
  • the "wrong" characters are unfortunately a given, they are not created, they are read so i need to convert them. – alex Feb 03 '14 at 19:05
  • What do you think would be quicker, to use an NSDictionary and use the objectForKey in the switch statement or to simply use "(int) c" to convert the character to an int and then use that in the switch statement? – alex Feb 03 '14 at 19:07

1 Answers1

0

usualy you could do

switch(c)
{
    case:'a':; // fall throught
    case:'b':
    {
        [NewName appendString:@"x"];
        break;
    }
}

but if you use 'Ú' you will get a compiler error, because this "char" is no unichar. you can try to set the int value for the char

switch(c)
{
    case: 218:; // 'Ú' , fall throught
    case: 186: // '°'
    {
        [NewName appendString:@"x"];
        break;
    }
    ...
}

I cannot test, because I cannot get a valid 'Ú'. Hoping for comments, maybe I can extend and answer upcomming questions ;)

geo
  • 1,781
  • 1
  • 18
  • 30
  • what do you mean you can't get a valid 'Ú' ?? – alex Feb 03 '14 at 14:56
  • The syntax in this answer is wrong. All of your `case` statements have an extra colon. And the cases that fall through do not need the semicolon. – rmaddy Feb 03 '14 at 15:29