I have this method:
class BottomPickerPanelView<T: Equatable>: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
private var data: [String: T]? = nil
public static func create<T: Equatable>(controller: UIViewController, values: [String: T]?) -> BottomPickerPanelView {
let view = Bundle.main.loadNibNamed("BottomPickerPanelView", owner: controller, options: nil)!.first as! BottomPickerPanelView
view.isHidden = true
view.pickerView.delegate = view
view.pickerView.dataSource = view
view.parentViewController = controller
view.data = values
return view
}
}
where 'view.data' is of type [String: T]? and T is, of course, a generic type. As you can see 'values' and 'data' are of the same type, but I get the following error: Cannot assign value of type '[String : T]?' to type '[String : _]?'
.
答案 0 :(得分:1)
When you use BottomPickerPanelView
without <T>
, the compiler can't know what the generic type is. Add <T>
to the return type and the cast and the assignment works:
class BottomPickerPanelView<T: Equatable>: UIView, UIPickerViewDelegate, UIPickerViewDataSource {
private var data: [String: T]? = nil
public static func create<T: Equatable>(controller: UIViewController, values: [String: T]?)
-> BottomPickerPanelView<T> {
let view = Bundle.main.loadNibNamed("BottomPickerPanelView", owner: controller, options: nil)!.first
as! BottomPickerPanelView<T>
view.isHidden = true
view.pickerView.delegate = view
view.pickerView.dataSource = view
view.parentViewController = controller
view.data = values
return view
}
}