I keep coming across (rare) cases where NSStrings don’t return equal although they look exactly the same when they are printed in the console. For instance:
[string1 isEqualToString:string2]
> comparing 'angelo debarre' to 'angelo debarre'
keeps returning NO. I’ve tried using NSString’s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] but still no luck. I also tried using comparators such as compare: and localizedCaseInsensitiveCompare: but I keep getting the same results. Any idea of something I might be overlooking?
Try decomposing the strings using one of the
NSStringUnicode normalization functions, like- (NSString *)decomposedStringWithCompatibilityMapping. Compatibility mapping (vs canonical mapping) should be what you want, as it decomposes visually similar character sequences into the same base components. I haven’t explicitly looked up whether a non-breaking space is normalized into a space, though.EDIT: There are two types of compatibility mapping normalization: decomposed and composed. The composed version is
- (NSString *)precomposedStringWithCompatibilityMapping. Not sure which one works better in general, but Unicode Standard Annex #15 (Unicode Normalization Forms) says the following of the KC/KD forms: