Home > Enterprise >  How to return multiple values from an if-else or a switch case
How to return multiple values from an if-else or a switch case

Time:09-21

I am still new and learning Swift so I was just wondering if there was some way I could write this code in a shorter way without repeating it thrice?

var meterCalc: Double {
    let lengthInput = Double(lengthIn1)   Double(lengthIn2)/100
    
    if lengthUnit == 0 {
        return lengthInput
    } else if lengthUnit == 1 {
        return lengthInput / 39.37
    } else {
        return lengthInput / 3.281
    }
}

var inchCalc: Double {
    let lengthInput = Double(lengthIn1)   Double(lengthIn2)/100
    
    if lengthUnit == 0 {
        return lengthInput * 39.37
    } else if lengthUnit == 1 {
        return lengthInput
    } else {
        return lengthInput * 12
    }
}

var feetCalc: Double {
    let lengthInput = Double(lengthIn1)   Double(lengthIn2)/100
    
    if lengthUnit == 0 {
        return lengthInput * 3.281
    } else if lengthUnit == 1 {
        return lengthInput / 12
    } else {
        return lengthInput
    }
}

I thought about using an if else statement every time lengthunit == 0, but I don't know how to return 3 different values that are used at 3 different places. So is there a way this code can be shortened? Or is there a way to return 3 different values from one if statement that can then be used at 3 different places?

CodePudding user response:

Let's start with using a Enum instead of random Int value where you'll have to remember yourself which one is meter, inch or feet.

enum LengthUnit {
    case meter
    case inch
    case feet
}

And then

var lengthUnit: LengthUnit = .meter //(or .inch, or .feet, depending on the initial unit)

Now, we can add a switch case, and in Swift, it can handles tuples:

func convert(_ value: Double, from initialUnit: LengthUnit, to targetUnit: targetUnit LengthUnit) -> Double {

    switch (initialUnit, targetUnit) {
        case (.meter, inch):
            return value / 39.37
        case (.inch, .meter):
            return value * 39.37
        case (.meter, .feet):
            return value / 3.281
        case (.feet, meter):
            return value * 3.281
        case (.feet, .inch):
            return value / 12
        case (.inch, .feet):
            return value * 12
        default: //It's (.inch, .inch), (.feet, .feet) and (.meter, .meter)
            return value
    }
}

Let's factor the line let lengthInput = Double(lengthIn1) Double(lengthIn2)/100 which is repeated each time into:

var combinedValues {
    return Double(lengthIn1)   Double(lengthIn2)/100
}

Then, in use:

var feetCalc: Double {
    return convert(combinedValue, from: lengthUnit, to .feet)
}

var inchCalc: Double {
    return convert(combinedValue, from: lengthUnit, to .inch)
}

var meterCalc: Double {
    return convert(combinedValue, from: lengthUnit, to .meter)
}

Now, there is already a converter for that Measurement in Foundation.framework

  • Related