I'm using a UIPickerView and its giving me the error (Cannot convert return expression of type '[String]' to return type 'String?' / UIPickerView). Here is my code.
// where the picker view is set up.
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let threeByThreeArray = ["OLL", "PLL"]
@IBOutlet weak var pickerViewOutlet: UIPickerView!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row]
default:
return getArrayForRow(row: row)
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> [String] {
switch row {
case 0:
return threeByThreeArray
default:
return cubesToWorkWith
}
}
}
}
and I get the error at the switch case inside the titleForRow at "return getArrayForRow(row: row)" thanks for any help in advance!!!!
so you are missing a few things:
first, you need to set delegate ad Datasource to self
and in the titleForRow
:
you need to return a string so your:
func getArrayForRow(row: Int) -> [String]
need to be like:
func getArrayForRow(row: Int) -> String
here is my offer:
class ViewController: UIViewController ,UIPickerViewDelegate,UIPickerViewDataSource{
// where the picker view is set up.
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let threeByThreeArray = ["OLL", "PLL"]
@IBOutlet weak var pickerViewOutlet: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.pickerViewOutlet.dataSource = self
self.pickerViewOutlet.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row] as String
default:
return getArrayForRow(row: row) as String
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> String {
switch row {
case 0:
return threeByThreeArray[row]
default:
return cubesToWorkWith[row]
}
}
}