ios - .包含闭包: 'Bool' is ambiguous without more context [Solution: Optional String issue]

标签 ios arrays swift closures contains

使用这个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/

相关文章:

iphone - 将两张图像合并为一张图像后,我的原始尺寸会改变

java - 字符串数组到十六进制数组 (Java)

ios - 如何删除根据重量结果生成的文本?

ios - Swift 3 写入文本文件

android - 相当于 Swift “extensions” 的 Dart 是什么?

ios - UIButton 在向其添加扩展后停止在界面生成器中显示 UIControl 操作

ios - 在 App-Store 中构建卡片高亮动画

ios - 使 UISearchBar 在两个不同的字段上进行搜索

java - 解析数组并提取相同长度的字符串

C - 我收到 "array size missing"错误,我是否声明数组错误?