我正在使用一個UIPickerView並給我錯誤(不能將類型'[String]'的返回表達式轉換爲返回類型'String?'/ 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
}
}
}
}
,我得到的錯誤在titleForRow開關內部的情況下,在「迴歸getArrayForRow(行:行)」 感謝提前任何幫助!!!!
'getArrayForRow'返回'[字符串]'和'pickerView?(_:titleForRow:...)''返回字符串'。你期望什麼?數組和字符串之間的魔術轉換? – user28434
P.S.這是從我的另一個問題的跟進問題在這裏:http://stackoverflow.com/questions/42316756/uipickerview-multi-components-in-swift –
'getArrayForRow(row:row)'返回一個'數組',但一個預計會有'String?'。我不明白預期的輸出,但'getArrayForRow(row:row).first'將修復崩潰,並希望可以幫助您理解問題:-) – shallowThought