2

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)
     }
}
Alex
  • 2,369
  • 3
  • 13
  • 23

3 Answers3

2

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)
         }
    }
}
Kevinosaurio
  • 1,952
  • 2
  • 15
  • 18
0

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 }
MichaelV
  • 1,231
  • 8
  • 10
0

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
    }
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34