Aaron Steinberg
Aaron Steinberg

Reputation: 53

Math in Multiple fields

Having trouble doing multiplication, division and addition in multiple fields that are populated by picker view selections. All of my picker views display the data but when I click the button to calculate, nothing happens then it errors out on the calculate function.

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var textColor: UITextField!
@IBOutlet weak var textRatio: UITextField!
@IBOutlet weak var textDensity: UITextField!
@IBOutlet weak var textRnumber: UITextField!
@IBOutlet weak var textWeight: UITextField!
@IBOutlet weak var textCdensity: UITextField!
@IBOutlet weak var textCatalyst: UITextField!
@IBAction func Calculate(_ sender: UIButton) {
    let a: Int? = Int(textColor.text!)
    let b: Int? = Int(textWeight.text!)
    let c: Int? = Int(textRnumber.text!)
    let d: Int? = Int(textCdensity.text!)
    let volume = b! / a!
    let total = volume * d!
    let catalyst = total * c!

    let catalysttext = String(catalyst)

    textCatalyst.text = catalysttext

}






var colorBase = ["E-100 Blackout","E-110 Midnight","E-120 Smoke", "E-130 Earth", "E-140 Jungle", "E-150 Sand", "E-160 Concrete", "E-170 Coyote M17", "E-180 Daniel Defense Deep Woods","E-190 20150", "H-109 Gloss Black", "H-112 Cobalt", "H-115 Sig Teal", "H-122 Gold", "H-125 ICON Grey", "H-126 Dewalt Yellow", "H-127 Kel Tec Blue", "H-128 Hunter Orange", "H-130 Combat Grey", "H-131 Dark Green", "H-133 Cross Canyon Arms Green", "H-135 Ratio Gold", "H-136 Snow White", "H-137 Gloss White", "H-139 Steel Grey", "H-140 Bright White", "H-141 Prison Pink", "H-142 Light Sand", "H-143 Benelli Sand", "H-144 Corvette Yellow", "H-145 Great White", "H-146 Graphite Black", "H-147 Satin Mag", "H-148 Burnt Bronze", "H-149 Copper Brown", "H-150 Savage Stainless", "H-151 Satin Aluminum", "H-151E Stain Aluminum Electrostatic", "H-152 Stainless", "H-153 Shimmer Gold", "H-154 Medium Brown", "H-155 Taurus Stainless", "H-157 Bright Nickel", "H-158 Shimmer Aluminum", "H-160 Colt Coyote", "H-166 Electric Yellow", "H-167 USMC Red", "H-168 Zombie Green", "H-169 Sky Blue", "H-170 Titanium", "H-171 NRA Blue", "H-172 Sea Blue", "H-175 Robin's Egg Blue", "H-184 Glock Grey", "H-185 Blue Titanium", "H-188 Magpul Stealth Grey", "H-189 Noveske Bazooka Green", "H-190 Armor Black", "H-196 A.I. Sage Green", "H-197 Wild Purple", "H-198 A.I. Pale Brown", "H-199 Desert Sand", "H-200 Highland Green", "H-201 McMillan Grey", "H-20180 FS Tan", "H-202 McMillan Olive", "H-203 McMillan Tan", "H-204 Multicam Green", "H-205 APA Sage", "H-206 Tarjac Green", "H-207 Wild Green", "H-208 Wild Pink", "H-210 Sig Dark Grey", "H-211 BAE Green", "H-212 Federal Brown", "H-213 Battleship Grey", "H-214 Smith's Grey", "H-215 Smith's Brown", "H-216 Smith and Wesson Red", "H-217 Bright Purple", "H-218 School Bus Yellow", "H-219 Gun Metal Grey", "H-220 Ridgeway Blue", "H-221 Crimson", "H-222 Peach Melba", "H-223 Rode Pink", "H-224  Sig Pink", "H-225 Mud Brown", "H-226 Patriot Brown", "H-227 Tactical Grey", "H-229 Sniper Green", "H-231 Magpul Foliage Green", "H-232 Magpul O.D. Green", "H-233 Benchmade O.D. Green", "H-234 Sniper Grey", "H-235 Coyote Tan", "H-236 O.D. Green", "H-237 Tungsten", "H-238 Midnight Blue", "H-239 Benchmade Coyote Tan", "H-240 Mil Spec O.D. Green", "H-241 GA O.D. Green", "H-242 Hidden White", "H-243 Safety Orange", "H-244 Bright Pink", "H-245 SOCOM Blue", "H-246 Desert Gold", "H-247 Desert Sage", "H-248 Forest Green", "H-249 Nut Brown", "H-250 A.I. Dark Earth", "H-251 Green Beret Green", "H-252 Midnight Green", "H-254 Muddy River", "H-255 Crushed Silver", "H-256 Desert Verde", "H-257 Texas Tan", "H-258 Chocolate Brown", "H-259 Barrett Bronze", "H-260 Standard ACU Green", "H-261 Austrian Flat Dark Earth", "H-262 Stone Grey", "H-263 Foliage Green", "H-264 Mil Spec Green", "H-265 Flat Dark Earth", "H-266 Red-Orange", "H-267 Magpul Flat Dark Earth", "H-268 Troy Coyote Tan", "H-269 Barrett Brown", "H-270 Daniel Defense Mil Spec+", "H-271 Daniel Defense Tornado", "H-272 Daniel Defense Deep Woods", "H-280 Multi-Regional Earth", "H-281 Woodland Tan", "H-294 Midnight Bronze", "H-295 Cobalt Kinetics Slate", "H-296 Cobalt Kinetics Green", "H-300 Armor Clear", "H-301 Matte Armor Clear", "H-30118 FS Field Drab", "H-302 Pearl Clear", "H-303 RotoBlue Clear", "H-30372 FS Brown Sand", "H-30372 FS Brown Sand(Big Blue)", "H-33446 FS Sabre Sand", "H-36357 FS Grey", "H-400 Jesse James Eastern Front Green", "H-401 Jessie James Civil Defense Blue", "H-402 Jesse James Cold War Grey", "H-7504M Matte Brown", "H-8000 RAL 8000", "H-900 Electrical Barrier"]

var outBase = ["1.33", "1.38", "1.38", "1.42", "1.40", "1.42", "1.38", "1.42", "1.40", "1.40", "1.34", "1.47", "1.10", "1.43", "1.41", "1.52", "1.39", "1.33", "1.41", "1.34", "1.36", "1.38", "1.43", "1.43", "1.41", "1.39", "1.39", "1.35", "1.36", "1.09", "1.52", "1.43", "1.38", "1.36", "1.36", "1.38", "1.36", "1.10", "1.40", "1.38", "1.40", "1.35", "1.12", "1.36", "1.36", "1.04", "1.37", "1.46", "1.42", "1.38", "1.39", "1.05", "1.48", "1.39", "1.43", "1.39", "1.36", "1.37", "1.43", "1.41", "1.38", "1.36", "1.38", "1.37", "1.36", "1.38", "1.37", "1.40", "1.40", "1.38", "1.05", "1.39", "1.37", "1.37", "1.35", "1.35", "1.40", "1.32", "1.36", "1.33", "1.36", "1.35", "1.36", "1.49", "1.40", "1.38", "1.41", "1.42", "1.37", "1.40", "1.36", "1.37", "1.37", "1.36", "1.37", "1.38", "1.36", "1.42", "1.39", "1.34", "1.37", "1.30", "1.38", "1.32", "1.38", "1.39", "1.35", "1.36", "1.37", "1.34", "1.39", "1.39", "1.38", "1.33", "1.31", "1.35", "1.35", "1.36", "1.38", "1.39", "1.38", "1.37", "1.44", "1.36", "1.39", "1.30", "1.37", "1.33", "1.35", "1.35", "1.48", "1.36", "1.45", "1.38", "1.38", "1.37", "1.38", "0.93", "0.92", "1.40", "0.93", "0.92", "1.36", "1.36", "1.40", "1.36", "1.37", "1.05", "1.04", "1.37", "1.40", "1.48"]

var ratioBase = ["H-Clear", "12:1", "13:1", "14:1", "15:1", "16:1", "17:1", "18:1", "19:1", "20:1", "21:1", "22:1", "23:1", "24:1"]

var rnumberBase = ["1/9", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", "1/24"]

var cdensityBase = ["1.05", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07"]

var picker = UIPickerView()
var picker2 = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()
    picker.delegate = self
    picker.dataSource = self
    textColor.inputView = picker
    picker2.delegate = self
    picker2.dataSource = self
    textRatio.inputView = picker2
    textCdensity.inputView = picker2
    self.picker.selectRow(32, inComponent: 0, animated: false)
    self.picker2.selectRow(7, inComponent: 0, animated: true)

    let toolBar = UIToolbar()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(doneClicked))

    toolBar.setItems([doneButton], animated: false)

    textColor.inputAccessoryView = toolBar
    textRatio.inputAccessoryView = toolBar
    textWeight.inputAccessoryView = toolBar

}

@objc func doneClicked() {
    view.endEditing(true)
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == picker{
    return colorBase.count
    }else if pickerView == picker2{
        return ratioBase.count
    }else if pickerView == picker2{
        return cdensityBase.count
    }
    return 1
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == picker{
    textDensity.text = outBase[row]
    };
    if pickerView == picker{
        textColor.text = colorBase[row]};
    if pickerView == picker2{
    textRnumber.text = rnumberBase[row]
    };
    if pickerView == picker2{
        textRatio.text = ratioBase[row]};
    if pickerView == picker2{
        textCdensity.text = cdensityBase[row]
    }
    }


func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == picker{
        return colorBase[row]
    }else if pickerView == picker2{
        return ratioBase[row]
    }else if pickerView == picker2{
        return cdensityBase[row]
    }
    return ""

    }

Upvotes: 0

Views: 65

Answers (2)

Aaron Steinberg
Aaron Steinberg

Reputation: 53

It turns out that it didn't like the fractions in the rnumberbase array. I converted them to decimal and it worked perfectly.

Upvotes: 1

Joakim Danielson
Joakim Danielson

Reputation: 51890

You set the textColor field to a text string like "E-110 Midnight" and then you try to convert that value to an integer, so I guess in your Calculate method a is always nil and probably b, c and d as well. You need to differentiate between what value to display and what value to calculate on.

May I suggest that you use structs or tuples for your data so you can more easily handle descriptions and values. This would also avoid you having to pickers for 5 arrays.

Upvotes: 0

Related Questions