Reputation: 61
I want to use characterAtIndex
in Objective-C to compare the character at a certain position and a character ('v' for example). The problem is that characterAtIndex
seems to send an int and not a character. I tried to do something like this:
if ([string characterAtIndex:i] = @"v")
or also:
if (string[[string characterAtIndex:i]] .... )
I know that in some language you can go directly to a certain position in the string (like an array) to see the character, but it doesn't seems to work that way in Objective-C.
Thanks for you help !
Upvotes: 2
Views: 5504
Reputation: 18290
I like @X-Slash's answer better (+1), but to be thorough I'll show how you would do this with only OOP cocoa (that is, with NSString's instead of c-strings).
if([[a substringWithRange:(NSRange){0,1}] isEqualToString:@"v"]) { ...
Where {0,1} is the first character, {1,1} is the second, etc.
Upvotes: 4
Reputation: 4131
characterAtIndex
returns a char, not NSString object, so do this kind of thing instead,
if ([a characterAtIndex:i] == 'v')
Upvotes: 8