Reputation: 2489
Using this code, I tried to do an insensitive-case search to find companies for a certain major, but I get the error "Expression 'Bool' is ambiguous without more context" at let isFound =
.
Why? How do I solve this?
company.majors
is a String
array. searchValue
is a lowercase String
let searchValue = filterOptItem.searchValue?.lowercased()
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ $0.caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
Upvotes: 2
Views: 4794
Reputation: 6023
Swift 5 , Swift 4
//MARK:- You will find the array when its filter in "filteredStrings" variable you can check it by count if count > 0 its means you have find the results
let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
let searchToSearch = "go"
let filteredStrings = itemsArray.filter({(item: String) -> Bool in
let stringMatch = item.lowercased().range(of: searchToSearch.lowercased())
return stringMatch != nil ? true : false
})
print(filteredStrings)
if (filteredStrings as NSArray).count > 0
{
//Record found
}
else
{
//Record Not found
}
Upvotes: 0
Reputation: 2010
SearchValue is an optional string.
If you are sure that searchValue can't be nil. Please use:
let isFound = company.majors.contains({ $0.caseInsensitiveCompare(searchValue!) == ComparisonResult.orderedSame })
If you are not sure, use:
if let searchValue = filterOptItem.searchValue?.lowercased(){
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ $0.caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
}
Upvotes: 2
Reputation: 1261
Depends very much of a context and possible extensions, however cod itself seems wrong: you should either implicit closure let isFound = company.majors.contains{ $0.caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
or use parameter name where:
.
Fixed code work fine for Swift4 playgrounds:
let company = (name: "Name", majors: ["a", "b", "cas"])
let searchValue = "a"
let isFound = company.majors.contains{ $0.caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
Upvotes: 0