0

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 :)

Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
RobbB
  • 1,214
  • 11
  • 39
  • `BinaryInteger` is a protocol not a type. You should use `Int`. Note that value expects a Double so there is no need to convert the value to integer. `var fromInches = Measurement(value: Double(convertInput.text!) ?? .zero, unit: .inches)` – Leo Dabus Aug 01 '20 at 02:14
  • Btw why are you using textview? You probably want a textfield. – Leo Dabus Aug 01 '20 at 02:25
  • Leo your code solves the first line errors but then the second line of the conversion throws: Cannot assign value of type '()' to type 'String'... I am confused, am i not understanding the Measurement method or is this a problem with the textField? ... I am using the textView for the unit conversion output and the textFeild for the unit value to be converted – RobbB Aug 01 '20 at 02:53
  • Do you need a textview? Is it editable? You can use a multi line label – Leo Dabus Aug 01 '20 at 03:08
  • You're right I could use a label but that doesn't fix the problem leading up to displaying the result of converting two values. Any other time I've gotten user input via a textField and inserted it into a method there was no issue. Now the "Measurement" is giving trouble... – RobbB Aug 01 '20 at 03:18
  • `fromInches.converted(to: .feet).value.description` – Leo Dabus Aug 01 '20 at 03:37
  • if you need to properly format the resulting value you can check this post https://stackoverflow.com/a/39860810/2303865 – Leo Dabus Aug 01 '20 at 03:39
  • I think that fixed it all. Now i find that my code does not do what i wanted. Back to the drawing board. Thanks for the help :) – RobbB Aug 01 '20 at 03:49

1 Answers1

0

I must have been very tired yesterday because i spotted my error today and I wanted to post the answer just in case someone else needs the code:

Basically my string inputs in the condition of the "IF" statement was missing capitol first letter... ;)

    func convert() {
    // Convert inches to feet
    if selectedFromUnit == "Inches" && selectedToUnit == "Feet" {
        let value = Double(convertInput.text!)!
        let fromInches = Measurement(value: value, unit: UnitLength.inches)
        let result = fromInches.converted(to: UnitLength.feet)
        print("\(result)")
    } else {
        print("nil")
        print("\(selectedFromUnit) to \(selectedToUnit)")
    }
}
RobbB
  • 1,214
  • 11
  • 39