NSPredicate evaluates wrong

let predicate = NSPredicate(format: "(SELF CONTAINS[cd] ',')") let predicate2 = NSPredicate(format: "(SELF CONTAINS ',')") let a = predicate.evaluate(with: ",") let b = predicate2.evaluate(with: ",") who knows why 'a' is true and 'b' is false, please tell me. I think 'a' should be false, is that i miss something.

Because predicate uses the d flag, which tells it to be diacritical insensitive, which enables a whole bunch of fuzzy matching. Your test uses two different commas, U+FF0C FULLWIDTH COMMA and U+002C COMMA, and those are considered the same when this fuzzy matching is enabled.

ps It’d help if you posted code snippets in a code block, per tip 5 in Quinn’s Top Ten DevForums Tips.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you for your answer, but use only c flag will return same result, is that prove d flag is not the reason cause that? Only when no c and d flag, the result will be the expectation i want. I want to keep c flag, please explain it more detail. And if i want to keep d flag two, what should i do to make different in this two commas, is this two commas are diacritical insensitive relationship?

use only c flag will return same result

Interesting. However, it doesn’t really change the story here. Comparing strings is fundamentally a locale-sensitive thing. Once you engage one of these flags, you opt in to a bunch of locale-sensitive behaviour. I’m kinda surprised by this specific behaviour, but it falls into the general category of “weird locale-sensitive string comparison behaviours”, which is a big category (-:

Can you explain more about the context here? Commas are obviously not case-sensitive so the simple workaround here is to drop both c and d. However, I suspect that won’t work for you because of big picture concerns. What are you using this predicate for?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NSPredicate evaluates wrong
 
 
Q