2017-02-21 31 views
1

我正在使用一個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(行:行)」 感謝提前任何幫助!!!!

+1

'getArrayForRow'返回'[字符串]'和'pickerView?(_:titleForRow:...)''返回字符串'。你期望什麼?數組和字符串之間的魔術轉換? – user28434

+0

P.S.這是從我的另一個問題的跟進問題在這裏:http://stackoverflow.com/questions/42316756/uipickerview-multi-components-in-swift –

+0

'getArrayForRow(row:row)'返回一個'數組',但一個預計會有'String?'。我不明白預期的輸出,但'getArrayForRow(row:row).first'將修復崩潰,並希望可以幫助您理解問題:-) – shallowThought

回答

1

所以你缺少一些東西 首先你需要設置代理廣告的數據源sekf 並在titleForRow:你需要返回一個字符串,因此你 FUNC getArrayForRow(行:智力) - > [字符串] 需要像 FUNC getArrayForRow(行:智力) - >字符串

這裏是我的建議:

類的ViewController:UIViewController中,UIPickerViewDelegate,UIPickerViewDataSource //其中選擇器視圖設置{了。 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] 
    } 

} 

}

+0

好吧,所以我改變了這個返回threeByThreeArray到這個返回threeByThreeArray [row] –

+0

做到了嗎?讓我知道 – NLU

+0

是的!感謝您的幫助! –

相關問題