neowinston
neowinston

Reputation: 7764

How to read NSString with accents?

I need to compare an NSString that that has foreign accents, like á, à, ã, ç, ô, é, í, etc., but logging the string messes up the characters that have the accents.

How do I pass the entire NSString for comparison, like this:

if (myString == @"Elevação")

    { 
     do something nice...    
    }

The logging I'm getting, using NSLog(@"myString = %@", myString); from the above code is myString = Eleva..o

Thanks for your help.

Upvotes: 0

Views: 276

Answers (3)

Frank Schmitt
Frank Schmitt

Reputation: 25775

Comparing two NSString pointers via == in Objective-C will only succeed if both sides of the comparison are the same instance of the same string.

If you want the expression to evaluate to true if the strings might be two different NSString instances with the same contents, you need to use NSString's isEqualToString: method (as MrMusic suggested).

I'm not sure how to print arbitrary unicode text to the log. You might consider temporarily adding a UILabel in your interface for debugging purposes and setting its text property to your string.

Upvotes: 1

Drew C
Drew C

Reputation: 6458

You want:

if ( [myString compare:@"Elevação" options:NSDiacriticInsensitiveSearch] == NSOrderedSame )

Upvotes: 3

SundayMonday
SundayMonday

Reputation: 19737

Try [someString isEqualToString:someOtherString];

Upvotes: 3

Related Questions