Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Possible To Increment A Letter, i.e A + 1 = B In Objective-C?

I am used to doing this in C or C++, ie:

myChar++;

should increment a letter.

I am trying to do the same in Objective-C, except that I have a NSString to start off with (the NSString is always just one letter). I have tried converting the NSString to a char *, but this method is deprecated and other ways of achieving this don't seem to work.

How should I convert an NSString to a char * - or, is there a way to increment a character in objective-c without needing a char * somehow?

Thanks :)

like image 394
Jordan Smith Avatar asked Dec 12 '22 16:12

Jordan Smith


1 Answers

// Get the first character as a UTF-16 (2-byte) character:
unichar c = [string characterAtIndex:0];
// Increment as usual:
c++;
// And to turn it into a 1-character string again:
[NSString stringWithCharacters:&c length:1];

Of course, this assumes incrementing a Unicode character makes sense, which does for ASCII-range characters but probably not for others.

like image 98
Daniel Dickison Avatar answered Jan 31 '23 05:01

Daniel Dickison