I want to display alert if myString
is present in myArray
:
var myString = "A"
var myArray = [A]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
But if the myString
is in opposite case, the alert is not showing :(
var myStr = "As"
var myArr = [AS]
if contains(self.arrayGroup, self.txtGroupName.text) {
let alert = UIAlertView()
alert.title = "Sorry :("
alert.message = "Duplicate Name"
alert.addButtonWithTitle("OK")
alert.show()
}
How can I find myString
in myArray
ignoring case?
I am guessing what you are looking for is something like the following:
let search = "b"
let arrayToSearch = ["A", "B", "C"]
let searchResult = arrayToSearch.filter() { $0.caseInsensitiveCompare(search) == NSComparisonResult.OrderedSame }
if searchResult.count != 0 {
print("'\(search)' matched to '\(searchResult[0])'")
} else {
print("'\(search)'not found")
}
which will output
'b' matched to 'B'