¿Cómo cambio el color del text de UIPickerView con varios componentes en Swift?

A continuación, el código cambiará el color de la fuente de la vista del selector de los 3 componentes. Sin embargo, falla cuando bash girar la rueda. Creo que tiene que ver con la function didSelectRow. ¿Tal vez las dos funciones tienen que estar anidadas de alguna manera? ¿Alguna idea?

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { var attributedString: NSAttributedString! if component == 0 { attributedString = NSAttributedString(string: a.text!, attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) } if component == 1 { attributedString = NSAttributedString(string: b.text!, attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) } if component == 2 { attributedString = NSAttributedString(string: c.text!, attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) } return attributedString } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){ switch component { case 0: aOutput.text = a[row] --> **Code breaks** case 1: bOutput.text = b[row] case 2: cOutput.text = c[row] default: 10 } 

    El siguiente pickerView:attributedTitleForRow:forComponent: implementación del método debería ayudarlo a:

     func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let attributedString = NSAttributedString(string: "some string", attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) return attributedString } 

    Actualizar

    Si desea utilizar attributedString en varias sentencias if o switch , el siguiente ejemplo de UIViewController lo ayudará a:

     import UIKit class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { @IBOutlet weak var picker: UIPickerView! let arrayOne = ["One", "Two", "Three", "Four", "Five", "Six"] let arrayTwo = ["Un", "Deux", "Trois", "Quatre", "Cinq", "Six"] let arrayThree = [1, 2, 3, 4, 5, 6] override func viewDidLoad() { super.viewDidLoad() picker.delegate = self picker.dataSource = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func numberOfComponentsInPickerView(_: UIPickerView) -> Int { return 3 } func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int { switch component { case 0: return arrayOne.count case 1: return arrayTwo.count case 2: return arrayThree.count default: return NSNotFound } } func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { var attributedString: NSAttributedString! switch component { case 0: attributedString = NSAttributedString(string: arrayOne[row], attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) case 1: attributedString = NSAttributedString(string: arrayTwo[row], attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) case 2: attributedString = NSAttributedString(string: toString(arrayThree[row]), attributes: [NSForegroundColorAttributeName : UIColor.networkingColor()]) default: attributedString = nil } return attributedString } func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) { switch component { case 0: println(arrayOne[row]) case 1: println(arrayTwo[row]) case 2: println(arrayThree[row]) default: break } } } 

    Swift 3

     func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let attributedString = NSAttributedString(string: "YOUR STRING", attributes: [NSForegroundColorAttributeName : UIColor.white]) return attributedString }