How to access switch results of a case?

Chéyo

How does one access the switch statement result of the example apple swift book code?

I guess I could make function that just returns the value of the case statement but I am trying to understand this code.

enum ServerResponse {
    case Result(String, String)
    case Error(String)
}

let success = ServerResponse.Result("6:00 am", "8:09 pm")
let failure = ServerResponse.Error("Out of cheese.")

switch success {
case let .Result(sunrise, sunset):
    let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."
case let .Error(error):
    let serverResponse = "Failure...  \(error)"
}
diazant

The example is so contrived that it's not at all helpful. Consider this

enum ServerResponse {
    case Result(String, String)
    case Error(String)
}

for i in 1...100 {
    let mySuccess: ServerResponse = {
        let zeroOrOne = rand() % 2
        if zeroOrOne == 0 {
            return ServerResponse.Result("7:00 am", "8.09 pm")
        } else {
            return ServerResponse.Error("Out of cheese.")
        }
    }()

    var serverResponse: String
    switch mySuccess {
    case let .Result(sunrise, sunset):
        serverResponse = "Sunrise is at \(sunrise) and sunset as \(sunset)"
    case let .Error(error):
        serverResponse = "Failure... \(error)"
    }

    println(serverResponse)
}

The gist is that the 'success' variable in the example should really be an assignment as a result of some function / subsystem call. I wrapped it in a loop for println() clarity

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access switch results of a case?

From Dev

While loop within switch case - Grouping results

From Dev

how to make switch case of enum case (swift)

From Dev

How to use switch case in my case?

From Dev

How to avoid switch case in factory

From Dev

How to properly return in a switch case?

From Dev

How to avoid switch case in Java?

From Dev

How to use switch case in for loop?

From Dev

How to use Java switch case

From Dev

how to use switch case like (if)?

From Dev

Can't access enum values inside Switch Case / Java

From Dev

How to build switch-case with variadic templates

From Dev

How to Refactor this type of switch-case statement?

From Java

How to implement switch-case statement in Kotlin

From Dev

How to improve this Ruby case switch statement?

From Dev

How to deal with default case in an enum based switch?

From Dev

C# switch case how to calculate

From Dev

How to use enum class in switch case

From Dev

how to use switch case(range of number) in jquery?

From Dev

how to switch-case with inconsistent variable format

From Dev

How to replace this switch - case with a Dictionary<string, Func<>>?

From Dev

How to avoid switch-case statements in Java

From Dev

How to replace switch-case to OOP

From Dev

How to avoid switch-case statements in Java

From Dev

How to concatenate foreach loop with **switch-case**

From Dev

How to use switch-case in Android actionbar?

From Dev

How do use a Switch Case Statement in Dart

From Dev

How do I reset switch case?

From Dev

How to equate characters using switch case in java?