使用数组填充 SwiftyPickerPopover 选择

Populate SwiftyPickerPopover choices with array

我正在使用 SwiftyPickerPopover 显示弹出控件,以便用户 select 一个值。之前在应用程序的另一个地方,我将其实现为:

let displayStringFor:((String?)->String?)? = { string in
            if let s = string {
                switch(s){
                case "Lhe”:
                    return "Lh”
                case "Khi":
                    return "Khi”
                case "Isb”:
                    return "Isb"
                case "Guj":
                    return "Guj"
                default:
                    return s
                }
            }
            return nil
        }

        let p = StringPickerPopover(title: "Select City", choices: ["Lhe”,”Khi”,”Isb”,”Guj”])
            .setDisplayStringFor(displayStringFor)
            .setDoneButton(
                action: {  popover, selectedRow, selectedString in

                    self.cityButton.setTitle(selectedString,for: .normal)
                    if selectedRow == 0 {
                        self.cityImage.image = UIImage(named: "Lhe")
                    } else if selectedRow == 1 {
                        self.cityImage.image = UIImage(named: "Khi")
                    } else if selectedRow == 2 {
                        self.cityImage.image = UIImage(named: "Isb")
                    } else if selectedRow == 3 {
                        self.cityImage.image = UIImage(named: "Guj")
                    }
            })
            .setCancelButton(action: {_, _, _ in
            })
        p.appear(originView: sender as! UIView, baseViewController: self)

您可以看到这里的值是硬编码的。现在我有一个 API 给了我所有的城市。所以我做了 var mainCitiesArray = [City]() 并调用了 API 并向它解析了数据。

mainCitiesArray 中,我现在拥有要在此弹出窗口中显示的所有城市。我该怎么做?

是否只需要将城市名称解析为字符串数组?

例如

let citiesNamesArray = mainCitiesArray.map({
    (city: City) -> String in
    return city.name
})

您可以将 citiesNamesArray 传递给 choises 参数。