MikeVamm  MikeVamm - 1 month ago 15
Swift Question

Calculate subtotal using tuples and 3 UIPickerviews

I'm a beginner in Swift programming. I have created 3 picker views and added some values from tuples. My challenge is I don't know how to separate the Doubles from the tuples and calculate the subtotal with quantity. Whatever the ticket type displayed on

Picker2
, I need to multiply it by the quantity from
Picker3
and display the subtotal.

Here is my coding.

import UIKit

class BuyTicketViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate{
@IBOutlet weak var Picker1: UIPickerView! = UIPickerView()
@IBOutlet weak var Picker2: UIPickerView! = UIPickerView()
@IBOutlet weak var Picker3: UIPickerView! = UIPickerView()
@IBOutlet weak var cityTextField: UITextField!
@IBOutlet weak var typeTextField: UITextField!
@IBOutlet weak var qtyTextField: UITextField!

var seletedItemArray = [(String,Double)]()
var Cities = ["Las Vegas","Los Angeles","New York"]
var typeLasVegas = [("City Tour $20.50", 20.50),("Museum $10.00", 10.00), ("Amusement Park $75.00",75.00)]
var typeLosAnglese = [("City Tour $29.99", 29.99),("Museum $15.00", 15.00), ("Amusement Park $79.99",79.99)]
var typeNewYork = [("City Tour $39.99", 39.99),("Museum $12.50", 12.50), ("Amusement Park $65.00",65.00)]

var Qt = [("1", 1.0),("2", 2.0),("3",3.0),("4", 4.0),("5",5.0),("6",6.0)]

var subTotal = 0.0
var ticketQty = 0
var ticketPrice = 0.0

override func viewDidLoad() {
super.viewDidLoad()

Picker1.isHidden = true
Picker2.isHidden = true
Picker3.isHidden = true

cityTextField.delegate = self
Picker1.delegate = self
typeTextField.delegate = self
Picker2.delegate = self
qtyTextField.delegate = self
Picker3.delegate = self

self.cityTextField.inputView = self.Picker1
self.typeTextField.inputView = self.Picker2
self.qtyTextField.inputView = self.Picker3

Picker1.tag = 0
Picker2.tag = 1
Picker3.tag = 2
// Do any additional setup after loading the view.
}

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0
{
return Cities[row]
}
else if pickerView.tag == 1
{
return seletedItemArray[row].0
}
else if pickerView.tag == 2
{
return Qt[row].0
}
return ""
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0
{
return Cities.count
}
else if pickerView.tag == 1
{
return seletedItemArray.count
}
else if pickerView.tag == 2
{
return Qt.count
}
return 1
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView.tag == 0
{
switch row {
case 0:
seletedItemArray = typeLasVegas
case 1:
seletedItemArray = typeLosAnglese
case 2:
seletedItemArray = typeNewYork

default:
seletedItemArray = []
}
Picker2.reloadAllComponents()
cityTextField.text = Cities[row]
Picker1.isHidden = true
Picker2.isHidden = false
}
else if pickerView.tag == 1
{
let item = seletedItemArray[row].0

if seletedItemArray[row] == typeLasVegas[row]
{
typeTextField.text = item

} else if seletedItemArray[row] == typeLosAnglese[row]
{
typeTextField.text = item
}else if seletedItemArray[row] == typeNewYork[row]
{
typeTextField.text = item
}

Picker2.isHidden = true
}
else if pickerView.tag == 2
{
qtyTextField.text = Qt[row].0
Picker3.isHidden = true
}
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == cityTextField
{
self.Picker1.isHidden = false
typeTextField.text = ""
qtyTextField.text = ""
}
else if textField == typeTextField
{
Picker2.isHidden = false
}
else if textField == qtyTextField
{
Picker3.isHidden = false
}
return false
}

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

Answer

Elements from a tuple are accessed as .0, .1, etc. This is how you would compute your subtotal:

let picker2row = Picker2.selectedRow(inComponent: 0)
let picker3row = Picker3.selectedRow(inComponent: 0)

let subtotal = selectedItemArray[picker2row].1 * Qt[picker3row].1

If you use named tuples:

var selectedItemArray = [(title: String, price: Double)]()
var typeLasVegas: [(title: String, price: Double)] = [("City Tour  $20.50", 20.50),("Museum  $10.00", 10.00), ("Amusement Park  $75.00",75.00)]
var Qt: [(title: String, quantity: Double)] = [("1", 1.0),("2", 2.0),("3",3.0),("4", 4.0),("5",5.0),("6",6.0)]

Then this is a little nicer:

let subtotal = selectedItemArray[picker2row].price * Qt[picker3row].quantity