3

I'm trying to figure out why my spinner won't hold integers. When I run the program, question marks appear in the spinner instead of 1,2,3,4 and so on all the way up to 100. I'm new to Swift and this is my very first time working with the spinner/Picker. My program is supposed to calculate Miles per Gallon, but that can't happen if I can't get the spinner to work, lol.

Here is the code that sets up the variables and the spinner:

class SecondViewController:
UIViewController,UIPickerViewDataSource,UIPickerViewDelegate
{
    var BlopSoundURL =  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Blop", ofType: "mp3")!)

    var soundAudioPlayer = AVAudioPlayer()

    var miles = 0.0
    var gallonsUsed = 0.0
    var mpg = 0.0

    // Variables for slider
    var minMiles = 1
    var maxMiles = 1000


    @IBOutlet weak var milesDrivenLabel: UILabel!

    @IBAction func milesDrivenSlider(sender: UISlider)
    {

    }

    @IBOutlet weak var gallonsUsedPicker: UIPickerView!
    let pickerData = [1,2,3,4,5,6,7,8,9,10,11,
                12,13,14,15,16,17,18,19,20,21,
                22,23,24,25,26,27,28,29,30,31,
                32,33,34,35,36,37,38,39,40,41,
                42,43,44,45,46,47,48,49,50,51,
                52,53,54,55,56,57,58,59,60,61,
                62,63,64,65,66,67,68,69,70,71,
                72,73,74,75,76,77,78,79,80,81,
                82,83,84,85,86,87,88,89,90,91,
                92,93,94,95,96,97,98,99,100]

There is no code for the submit button yet, but here is the rest of the code for the spinner and the program:

@IBAction func submitButton(sender: UIButton)
{
    soundAudioPlayer.play()
    let row = gallonsUsedPicker.selectedRowInComponent(0)

}



func numberOfComponentsInPickerView(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) -> Int!
{
    return pickerData[row]
}


override func viewDidLoad()
{
    super.viewDidLoad()

    gallonsUsedPicker.dataSource = self
    gallonsUsedPicker.delegate = self

    soundAudioPlayer = AVAudioPlayer(contentsOfURL: BlopSoundURL, error: nil)
    // Do any additional setup after loading the view, typically from a nib.
}   // End of viewDidLoad

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


}   // End of SecondViewController

I'm really not sure what the code in the functions do because the book didn't do a very good job of explaining it. Then, I'll have to figure out how to take the number from the spinner and store it in a variable, so that I can do the mpg calculation. I know how to do it with a slider, but not a spinner. Is it basically the same thing? I have a feeling that this is going to get interesting. Thank you, your help is much appreciated.

1 Answers1

1

First by Swift convention you should name your vars starting with a lowercase letter and use URLForResource(withExtension:) method to find out an url of a local resource:

let blopSoundURL = NSBundle.mainBundle().URLForResource("Blop", withExtension: "mp3")!

Second the picker values should be String:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return "\(row+1)"
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return 100
}

And you can use

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // do whatever with row+1  here
}

// or 

@IBAction func submitButton(sender: UIButton) {
    soundAudioPlayer.play()
    let rowValue = gallonsUsedPicker.selectedRowInComponent(0) + 1

}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • You should never use the `description` method for anything other than debug logging – Paulw11 May 04 '15 at 01:46
  • I don't think there is a problem but I can use String Interpolation if you prefer – Leo Dabus May 04 '15 at 01:47
  • Okay, for the very last function you posted (the one with "do whatever with row+1 here", I put "gallonsUsed = (row+1)" to store the value of the spinner into gallonsUsed. Is what I did correct? – InSeriousNeedOfAspirin May 04 '15 at 01:52
  • Yes it is correct but You can keep your code let rowValue = gallonsUsedPicker.selectedRowInComponent(0) + 1 – Leo Dabus May 04 '15 at 01:53
  • @LeonardoSavioDabus Would that go inside of the function underneath gallonsUsed = (row+1) or outside of the function? (Sorry for all the questions) – InSeriousNeedOfAspirin May 04 '15 at 01:55
  • @LeonardoSavioDabus Okay, thank you. I'll give that a try and then code the rest of the program to see if it works. I'll let you know of my results. Again, thank you (and thanks for your patients, lol). – InSeriousNeedOfAspirin May 04 '15 at 02:00