使用这个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 不能为零。请使用:
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)
}
}
}
关于ios - .包含闭包: 'Bool' is ambiguous without more context [Solution: Optional String issue],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48268446/