0

I am using a uipickerview to choose songs from. The problem is when I press play without interacting with the picker view (i.e trying to play the first song that is already selected/loaded by the picker view.) I get an error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I have the code below set for didSelectRow:

     func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

            musicChoice = songs[row]
    }

And as I understand, this only initiates once the user scrolls/ interacts with the picker view - so I am trying to use selectedRow somehow but am messing up, any ideas?

Updated pickerview code below:

var songs:[String] = []
var musicChoice = "Something"

let foods = ["Apples", "Bananas", "Corn", "Beans", "Tomatoes"]

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

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

}

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



func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    musicChoice = songs[row]

}
Berro
  • 15
  • 5
  • Does this answer your question? [How to set a default Value of a UIPickerView](https://stackoverflow.com/questions/11777072/how-to-set-a-default-value-of-a-uipickerview) – pkamb Sep 17 '21 at 02:12

1 Answers1

1

The did select row method is self explanatory it says didSelectRow so you are right without interacting with the rows it doesn't initialise any thing.

Instead of selected row you can always initialise the first songs value to be played. i.e

musicChoice = songs[0]

while you declare the musicChoice variable. That would solve the purpose.

Jeet
  • 5,569
  • 8
  • 43
  • 75
  • I tried to do that, but it says I can only use song[0] after the picker view code, not before where I declare the musicChoice variable, I've updated my original post to show. – Berro Dec 02 '17 at 16:25
  • By what I understand your `playSong` method should be completely different from you picker view delegates code, hence you should be able to use the songs[0] without even touching the picker view if you initialise it before. Can you please share the complete code of the UIViewController class to we can have a look at it? – Jeet Dec 02 '17 at 16:29
  • Was about to copy paste, then thought I'd add your suggestion to right after the songs are appended to the songs array and that seems to have done the trick. This way, after the songs are added to the songs array, musicChoice is automatically filled to be the first entry in the songs array (which should always be the first song) and this will always be what will always displayed as the first option in the picker view - or am I mistaken? – Berro Dec 02 '17 at 17:05
  • Looks like everything is working for now but I got to run some more tests tomorrow and double check on other devices. – Berro Dec 02 '17 at 17:25