我的项目是一个有很多注释点的 map ,用户可以通过pickerView来查找特定的注释。一切都很好,除了所有注释点的列表似乎是随机显示的。我想按字母顺序对注释进行排序。
这是我当前的工作代码:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.mapView.annotations[row].title ?? "No title"
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true)
}
我尝试过实现这个,但没有任何成功...
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let sortedNames = self.mapView.annotations.sorted(by: {$0.title < $1.title})
return sortedNames[row].title ?? "No title"
}
我遇到了这个错误:
Cannot convert value of type 'String??' to expected argument type 'UIContentSizeCategory'
任何提示将不胜感激。
最佳答案
我知道这个问题回答起来有点老了,但对于其他有问题的人来说,问题是字符串不能是可选的。修复它以提供默认值或强制打开它。例如,这将是问题的一种解决方案:
let sortedNames = self.mapView.annotations.sorted(by: { ($0.title ?? "") < ($1.title ?? "") }))
return sortedNames[row].title ?? "No title"
关于swift - 如何按字母顺序对注释数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532022/