I apologize in advance, as I'm very new to Swift programming and Xcode, but I'm trying to build an application for my Senior Project in school. My issue stems from being able to use data from a picker view to then assign a value to a variable, based on what was selected in the picker view. For example, when "Aluminum 80" is selected in picker view, I want it to assign 0.399 to the FV variable. I'm not sure how to accomplish this, and I've tried multiple different methods, but nothing seems to work. I thought it would work directly from "data" and using my if...else structure, but it's just defaulting to the 0 that I initialized FV too earlier.
I'd also like to apologize if my code looks sloppy, as I'm still learning. My segment button is still a work in progress. Everything else works as intended though. Thanks in advance
I'd also like to apologize if my code looks sloppy, as I'm still learning. My segment button is still a work in progress. Everything else works as intended though. Thanks in advance
Code Block // // ScubaViewController.swift // Navy Diving Air Calculations // // Created by Tyler Ault on 5/15/21. // import UIKit class ScubaViewController: UIViewController{ let consumptionScuba:Double = 1.4 var allowedTime:Double = 0 var consumptionAir:Double = 0 var volumeAvailable:Double = 0 var mmP:Double = 250 var FV:Double = 0.0 var cylN:Double = 1 let data = ["Aluminum 100", "Aluminum 80", "Aluminum 63", "Aluminum 50", "Steel 120", "Steel 100", "Steel 72"] override func viewDidLoad() { super.viewDidLoad() picker.dataSource = self picker.delegate = self } @IBOutlet var picker: UIPickerView! @IBOutlet var plannedDepth: UITextField! @IBOutlet var cylinderPSI: UITextField! @IBOutlet var timeOutput: UILabel! @IBAction func didChangeSegment (_ sender: UISegmentedControl){ if sender.selectedSegmentIndex == 0 { cylN = 1 } else if sender.selectedSegmentIndex == 1 { cylN = 2 } } @IBAction func btnCalcClicked(){ //The code below this is to perform the necessary air calculations. The if...else structure assigns the appropriate floodable volume dependant on the air cylinder type and size selected from the picker, and then assigns the value from the string to FV. The calculations below this plug in all other necessary values if data == ["Aluminum 100"]{ FV = 0.47 }else if data == ["Aluminum 80"]{ FV = 0.399 }else if data == ["Aluminum 63"]{ FV = 0.319 }else if data == ["Aluminum 50"]{ FV = 0.281 }else if data == ["Steel 120"]{ FV = 0.526 }else if data == ["Steel 100"]{ FV = 0.445 }else if data == ["Steel 72"]{ FV = 0.42 } consumptionAir = Double(((Double(plannedDepth.text!)! + 33)/33)) * 1.4 volumeAvailable = Double((Double(cylinderPSI.text!)! - mmP) / 14.7) * FV * cylN allowedTime = volumeAvailable / consumptionAir timeOutput.text = String(allowedTime) //Outputs the time allowed for the dive, in minutes, based on all factors input. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } extension ScubaViewController: UIPickerViewDataSource { func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return data.count } } extension ScubaViewController: UIPickerViewDelegate{ func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent: Int) -> String? { return data[row] } }