Search code examples
iosswiftcore-datauipickerviewios10

Can't set pickerView titleForRow as (String?, String?)


I'm using CoreData and I'm trying to have 2 different columns in my UIPickerView with Stores and the Item Type

When I run it, the items are "there" but they appear as a "?"

here is my code for my titleForRow function:

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> (String?, String?) {
    //title
    let store = stores[row]
    let itemType = itemTypes[row]

    // select store out of array of stores
    return (store.name, itemType.type)
}

something to note is Xcode is giving me a warning saying:

Instance method 'pickerVIew(titleForRow:forComponent:)' nearly matches optional requirement 'pickerView(titleForRow:forComponent:)' of protocol UIPickerView


Solution

  • You can return only String type, not tuple. If you want to add two columns, you need to set

    pickerView.numberOfComponents = 2
    

    and check component parameter like:

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return component == 0 ? stores[row].name : itemTypes[row].type
    }