I am trying to add a basic conversion method using if statements and two pickerViews. For example, I'm trying to convert say "inches" to "feet" or "millimeters" but when I try and use the textField as input in the conversion initialization I get different errors like:
Something like "cannot convert type String or Double to expected type BinaryInteger Cannot assign to property: 'self' is immutable
So far I have tried the following and many more:
var fromInches = Measurement(value: Double(exactly: BinaryInteger(convertInput.text)), Double, unit: UnitLength.inches)
var fromInches = Measurement(value: Double(convertInput.text), Double, unit: UnitLength.inches)
Full code (excluding unnecessary):
import UIKit
class MetricImperialConvertViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var convertFrom: UIPickerView!
@IBOutlet weak var convertTo: UIPickerView!
@IBOutlet weak var convertInput: UITextField!
@IBOutlet weak var convertOutput: UITextView!
var selectedFromUnit = ""
var selectedToUnit = ""
// Convert from picker tag is #1
var convertFromData = ["Inches", "Feet", "Millimeters", "Centimeters", "Meters"]
//Convert to picker tag is #2
var convertToData = ["Inches", "Feet", "Millimeters", "Centimeters", "Meters"]
...
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1 {
selectedFromUnit = convertFromData[row]
} else if pickerView.tag == 2 {
selectedToUnit = convertToData[row]
}
print("Convert from: \(selectedFromUnit)\n to: \(selectedToUnit)")
// Convert inches to feet
if selectedFromUnit == "inches" && selectedToUnit == "feet" {
var fromInches = Measurement(value: Double(convertInput.text), Double, unit: UnitLength.inches)
convertOutput.text = fromInches.converted(to: UnitLength.feet)
}
}
This thing is driving me nuts, I'm way too new at code to deal with ultra complexity. Please help :)