J. Doe
J. Doe

Reputation: 563

How to populate UIPickeView from dictionary?

I have this pickerData:

var pickerData: (id: String, value: [String: String])?

when I tap on UITextField I do the next:

@IBAction func whoCanSee(_ sender: UITextField) {
    pickerData = (id: "viewOption", value: ["F": "Me and my friends", "M": "Only certain users", "E": "Every user"])

    sender.inputView = self.pickerView
}

and later in

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int)

I'm trying to fetch values from value in my tuple as:

self.pickerData!.value.values[row]

but it returns me this error:

Cannot subscript a value of type 'LazyMapCollection<Dictionary<String, String>, String>' with an index of type 'Int'

I have tried also with for...in but in this keys obviously I get always the first element, as each time this block is calling again and again.

How can I solve my problem?

Upvotes: 0

Views: 149

Answers (1)

Fangming
Fangming

Reputation: 25260

You are using a dictionary so you cannot just give it an index. Firs

let arr = Array(self.pickerData!.value)
print(arr[row])

Upvotes: 1

Related Questions