Your Solution would work for:
myValue
is representing valid regex pattern.
myValue
may not contain single quotes ('
).
If myValue
may contain single quote, this would work:
request.predicate = NSPredicate(format: "entityProperty MATCHES [c] %@", "[^1234]" + myValue)
And if myValue
is not a regex pattern and may contain some meta-characters, you may need to write something like this:
request.predicate = NSPredicate(format: "entityProperty MATCHES [c] %@", "[^1234]"+NSRegularExpression.escapedPattern(for: myValue))
Anyway, you need to remember:
Single or double quoting variables (or substitution variable strings)
cause %@, %K, or $variable to be interpreted as a literal in the
format string and so prevent any substitution.
Parser Basics (Predicate Programming Guide)
(I used NSPredicate
as Predicate
is not available in Xcode 8 beta 6.)