How can I check the enum using the if statement in swift?

Today I wrote an application that should correct some Doublefor a number of significant digits / digits (sig fig). User can set no. sig fig to convert to. I store this information in NSUserDefaults.

I created an enumeration to represent these parameters because I want to make my code more readable, and not just old integers. Here is the listing:

enum SigFigOptions{
    case No
    case Yes(Int)
}

If so .No, the numbers will be accurate. If so .Yes, the numbers will be corrected. And the sig fig number is stored in the corresponding value.

Then I created a method inside an enumeration called correctTo.

func correctTo (i: Double) -> Double {
    if self == .No {

    }
}

When I wrote this, I saw that there was a syntax error saying that it was ambiguous. So I changed it to:

if self == SigFigOptions.No

, == SigFigOptions.

! , switch. , switch , . , if . !

, enums if?

, , -. , .

+4
1

: if case .No = self { ... }

+16

Source: https://habr.com/ru/post/1624644/


All Articles