Linked Questions

Popular Questions

swift - How sort alphabetically annotations array

Asked by At

My project is a map with a lot of annotations points, and user could look for a specific annotation thanks to a pickerView. All is fine except the list of all annotations points seems randomly displayed. I would like to sort the annotations alphabetically.

Here s my current working code :

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)
    }

I ve tried to implement this, but without any success...

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"

    }

I ve got this error:

Cannot convert value of type 'String??' to expected argument type 'UIContentSizeCategory'

Any hint would be greatly appreciated.

Related Questions