热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

迅捷-如何按字母顺序排列注释数组

如何解决《迅捷-如何按字母顺序排列注释数组》经验,为你挑选了1个好方法。

我的项目是带有很多注释点的地图,由于有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"

    }

我有这个错误:

无法转换类型为'String ??'的值 到预期的参数类型'UIContentSizeCategory'

任何提示将不胜感激。



1> 小智..:

我知道这个问题回答有点老了,但是对于其他有问题的人来说,问题是String不能是可选的。修复它以提供默认值或强制解开它。例如,这将是该问题的一种解决方案:

let sortedNames = self.mapView.annotations.sorted(by: { ($0.title ?? "") <($1.title ?? "") }))
return sortedNames[row].title ?? "No title"


推荐阅读
author-avatar
舞倾城2010_217
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有