如何从字典中填充UIPickeView?

时间:2021-08-12 15:44:39

I have this pickerData:

我有这个pickerData:

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

when I tap on UITextField I do the next:

当我点击UITextField时,我会做下一个:

@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.

我也尝试过for ... in但是在这个键中我显然总是第一个元素,因为每次这个块一次又一次地调用。

How can I solve my problem?

我怎样才能解决我的问题?

1 个解决方案

#1


1  

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

您正在使用字典,因此您不能只给它一个索引。杉杉

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

#1


1  

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

您正在使用字典,因此您不能只给它一个索引。杉杉

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