使用此code,我尝试进行不区分大小写的搜索以找到某个专业的公司,但是在let isFound =
上出现错误“表达式'Bool'是模棱两可的,没有更多上下文”。
为什么?我该如何解决? company.majors
是一个String
数组。 searchValue
是小写的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)
}
}
最佳答案
SearchValue是一个可选字符串。
如果您确定searchValue不能为nil。请用:
let isFound = company.majors.contains({ $0.caseInsensitiveCompare(searchValue!) == ComparisonResult.orderedSame })
如果不确定,请使用:
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)
}
}
}