Search code examples
arraysswiftuipickerview

Display names of APIs in a picker view in Swift


I want to display the names for an array of APIs in a picker view.

Example: "fox-sports" displayed as FOX Sports on the picker view.

I'm currently placing an array of the API names.

var pickerData = ["SELECT A SOURCE", "espn", "nfl-news", "fox-sports", "bbc-
sport", "the-sport-bible", "sky-sports-news"]

Solution

  • I think you can use Dictionary to store API names:

    let channels = [
        "espn": "ESPN",
        "fox-sports": "FOX Sports",
    ]
    let pickerData = ["SELECT A SOURCE"] + channels.values
    

    And when you want to use keys: channels.keys