![](/img/trans.png)
[英]Instance method 'contains' requires that 'UITextField' conform to 'StringProtocol'
[英]Swift Array Sort by Property Error : Instance method 'localizedCaseInsensitiveCompare' requires that 'String?' conform to 'StringProtocol'
我正在嘗試對這個自定義對象數組進行排序,但出現以下錯誤:
實例方法“localizedCaseInsensitiveCompare”需要“String?” 符合 'StringProtocol'
filteredCountriesAndCities?.sorted(by: {$0.countryName?.localizedCaseInsensitiveCompare($1.countryName) == ComparisonResult.orderedAscending})
您需要處理countryName
為 nil 的情況,這里是一個最后排序 nil 值的示例
let sorted = array.sorted {
guard let first = $0.countryName else {
return false
}
guard let second = $1.countryName else {
return true
}
return first.localizedCaseInsensitiveCompare(second) == ComparisonResult.orderedAscending
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.