-1

I'm trying to make items show up on my pickerView, I can't seem to figure out why its not showing up. Any Suggestions?

import UIKit

class InspectionViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    @IBOutlet weak var picker: UIPickerView!

    var pickerData: [String] = [String]();

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // Connect data:
        self.picker.delegate = self;
        self.picker.dataSource = self;


        // Input data into the Array:
        pickerData = ["53W4", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"];
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        return pickerData[row]
    }


    /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */

}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Signature of `titleForRow` is changed compare with this one https://stackoverflow.com/questions/39933644/xcode-8-swift-3-simple-uipicker-code-not-working – Nirav D Jun 05 '17 at 11:29
  • Don't you get a *... nearly matches optional requirement ...* warning and a fix suggestion (actually 3 suggestions) ? – vadian Jun 05 '17 at 11:37

1 Answers1

0

You should use this:

func pickerView(
    _ pickerView: UIPickerView,
    titleForRow row: Int,
    forComponent component: Int
) -> String? {
  pickerData[row]
}

instead of:

func pickerView(
  pickerView: UIPickerView,
  titleForRow row: Int,
  forComponent component: Int
) -> String! {
  pickerData[row]
}

The diff is in Omitting Argument Labels. Here is a doc link

Tarek A.
  • 218
  • 2
  • 12