.Contains 闭包:'Bool' is ambiguous without more context [解决方案:可选字符串问题]
.Contains closure: 'Bool' is ambiguous without more context [Solution: Optional String issue]
使用此 ,我尝试进行不区分大小写的搜索以查找某个专业的公司,但我在 let isFound =
处收到错误 "Expression 'Bool' is ambiguous without more context"。
为什么?我该如何解决?
company.majors
是一个 String
数组。 searchValue
是小写 String
let searchValue = filterOptItem.searchValue?.lowercased()
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
SearchValue 是可选字符串。
如果您确定searchValue 不能为nil。请使用:
let isFound = company.majors.contains({ [=10=].caseInsensitiveCompare(searchValue!) == ComparisonResult.orderedSame })
如果您不确定,请使用:
if let searchValue = filterOptItem.searchValue?.lowercased(){
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
}
很大程度上取决于上下文和可能的扩展,但是 cod 本身似乎是错误的:您应该隐式闭包 let isFound = company.majors.contains{ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
或使用参数名称 where:
.
修复了适用于 Swift4 游乐场的代码:
let company = (name: "Name", majors: ["a", "b", "cas"])
let searchValue = "a"
let isFound = company.majors.contains{ [=10=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
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
}
使用此 let isFound =
处收到错误 "Expression 'Bool' is ambiguous without more context"。
为什么?我该如何解决?
company.majors
是一个 String
数组。 searchValue
是小写 String
let searchValue = filterOptItem.searchValue?.lowercased()
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
SearchValue 是可选字符串。
如果您确定searchValue 不能为nil。请使用:
let isFound = company.majors.contains({ [=10=].caseInsensitiveCompare(searchValue!) == ComparisonResult.orderedSame })
如果您不确定,请使用:
if let searchValue = filterOptItem.searchValue?.lowercased(){
for company in allCompanies {
//Insensitive case search
let isFound = company.majors.contains({ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame })
if (isFound) {
filteredCompanies.insert(company)
}
}
}
很大程度上取决于上下文和可能的扩展,但是 cod 本身似乎是错误的:您应该隐式闭包 let isFound = company.majors.contains{ [=11=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
或使用参数名称 where:
.
修复了适用于 Swift4 游乐场的代码:
let company = (name: "Name", majors: ["a", "b", "cas"])
let searchValue = "a"
let isFound = company.majors.contains{ [=10=].caseInsensitiveCompare(searchValue) == ComparisonResult.orderedSame }
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
}