I need to use NSPredicate to match two strings, case-insensitive, diacritic insensitive, and whitespace-insensitive.
The predicate would look something
Swift 4:
func ignoreWhiteSpacesPredicate(id: String) -> NSPredicate {
return NSPredicate(block: { (evel, binding) -> Bool in
guard let str = evel as? String else {return false}
let strippedString = str.components(separatedBy: CharacterSet.whitespaces).joined(separator: "")
let strippedKey = id.components(separatedBy: CharacterSet.whitespaces).joined(separator: "")
return strippedString.caseInsensitiveCompare(strippedKey) == ComparisonResult.orderedSame
})
}
Example:
let testArray:NSArray = ["abc", "a bc", "A B C", "AB", "a B d", "A bC"]
let filteredArray = testArray.filtered(using: ignoreWhiteSpacesPredicate(id: "a B C"))
Result:
["abc", "a bc", "A B C", "A bC"]