gpt4 book ai didi

swift - 无法将类型 '[String]' 的返回表达式转换为返回类型 'String?'/UIPickerView

转载 作者:行者123 更新时间:2023-11-28 06:24:37 25 4
gpt4 key购买 nike

我正在使用 UIPickerView,它给我错误(无法将类型“[String]”的返回表达式转换为返回类型“String?”/UIPickerView)。这是我的代码。

// 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
}

}
}
}

我在“return getArrayForRow(row: row)”处的 titleForRow 内的 switch case 处收到错误感谢您提前提供帮助!!!!

最佳答案

所以你错过了一些东西:

首先,你需要设置delegate ad Datasource为self在 titleForRow 中:

您需要返回一个字符串,以便您:

func getArrayForRow(row: Int)  -> [String]

需要像:

func getArrayForRow(row: Int)  -> String

这是我的报价:

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]
}

}

}

关于swift - 无法将类型 '[String]' 的返回表达式转换为返回类型 'String?'/UIPickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com