[英]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.