Swift - Chapter 5 - Switch - Where Clauses section


#1

Book - 2nd Edition Swift Programming
pg. 42
Using xcode V-8.2

At the bottom of the Switch statement on the page you will see this:

case let unknownCode where (unknownCode >= 200 && unknownCode < 300)
** || unknownCode > 505:**

So this is what I worked out, might not be pretty but it Works! (all is on one line)

case let unknownCode where (unknownCode >= 200 || unknownCode <= 300 || unknownCode > 505): break

Hope this helps anyone who has trouble with this page, if anyone has a better solution please let me know.


#2

That line of code needs a review. Please post the reviewed version.


#3

Thanks ibex. :wink: I edited it.


#4

Would you happen to have a better solution, something more concise or well structured perhaps ibex?


#5

I don’t have the book, but if you post the whole code, I can get a better picture of what you are trying to do.


#6

Ibex, I found that I was missing some code! (Totally my mess up Not the book) But when I made that mistake I found that my solution still populated a response in the review area… and I have No idea why!!.. maybe you can tell me. I copied everything below and tried to be as thorough as possible. Please look for comments //

var statusCode: Int = 400
var uknownCode: String = "The request failed with the error:"
var errorString: String = “The request failed with the error:”

switch statusCode {

case 100, 101:
errorString += “Informational, (statusCode)”

case 204:
errorString += “Successful but no content, 204.”

case 300…307:
errorString += “Redirection (statusCode).”

case 400…417:
errorString += “Client error, (statusCode).”

case 500…505:
errorString += “Server error, (statusCode)”

case let unknownCode where (unknownCode >= 200 && unknownCode < 300) || unknownCode > 505:
// — line of code just above "case let … 505: is book version
// – just below “errorString … code.” I did not type in my code!
// – plz look below for new section to see what I did

errorString = "\(unknownCode) is not a known error code."

default:
errorString = “Unexpected error encountered.”

}

// --> New Section

var statusCode: Int = 400
var uknownCode: String = "The request failed with the error:"
var errorString: String = “The request failed with the error:”

switch statusCode {

case 100, 101:
errorString += “Informational, (statusCode)”

case 204:
errorString += “Successful but no content, 204.”

case 300…307:
errorString += “Redirection (statusCode).”

case 400…417:
errorString += “Client error, (statusCode).”

case 500…505:
errorString += “Server error, (statusCode)”

case let unknownCode where (unknownCode >= 200 && unknownCode < 300 || unknownCode > 505): break
// — line just above from “case let … 505): break” is my adjustment
// – Also below I did not enter the code "Error String = “(unknownCode) is not a known error code.”

default:
errorString = “Unexpected error encountered.”

}

// —> Review Area response (response not a flagged error)
“The request failed with the error:Client error, 400.”

// Now there is an error that is thrown when you type the books version and make the mistake I made of leaving out "Error String = “((unknownCode) is not a known error code.” this is the error ----> ‘case’ label in a ‘switch’ should have at least one executable statement
I am guessing its because of the statement I omitted, but then why didn’t it throw an error with my version when I Did omit that statement?

Thanks :wink:


#7

There is really no need for that case let unknownCode where… clause because the default clause will catch all the unhandled cases.

func describe (statusCode : Int) -> String {
    var errorString : String = "The request failed with the error:"
    
    switch statusCode {
        
    case 100, 101:
        errorString += "Informational \(statusCode)"
        
    case 204:
        errorString += "Successful but no content \(statusCode)"
        
    case 300...307:
        errorString += "Redirection \(statusCode)"
        
    case 400...417:
        errorString += "Client error \(statusCode)"
        
    case 500...505:
        errorString += "Server error \(statusCode)"
        
    default:
        // catch all other cases
        errorString = "Unexpected error encountered \(statusCode)"
    }
    return "\(#function): \(errorString)"
}

let codes = [0, 99, 100, 102, 103, 104, 105, 204, 300, 400, 500, 506]
for code in codes {
    let rv = describe (statusCode: code)
    print ("\(code): \(rv)")
}

However, if there really is a need for it, then better to make it cover all other unhandled cases in the range [100, 505].

func describe2 (statusCode : Int) -> String {
    var errorString : String = "The request failed with the error:"
    
    switch statusCode {
        
    case 100, 101:
        errorString += "Informational \(statusCode)"
        
    case 204:
        errorString += "Successful but no content \(statusCode)"
        
    case 300...307:
        errorString += "Redirection \(statusCode)"
        
    case 400...417:
        errorString += "Client error \(statusCode)"
        
    case 500...505:
        errorString += "Server error \(statusCode)"
        
    case let unknownCode where (unknownCode >= 100 && unknownCode <= 505):
        // unhandled cases above
        errorString = "\(unknownCode) is not a known error code."

    default:
        // all other cases
        errorString = "Unexpected error encountered \(statusCode)"
    }
    return "\(#function): \(errorString)"
}

let codes = [0, 99, 100, 102, 103, 104, 105, 204, 300, 400, 500, 506]
for code in codes {
    let rv = describe2 (statusCode: code)
    print ("\(code): \(rv)")
}

#8

Sorry for the delayed response, thank you for clarifying Ibex :smiley:
I’m curious for the constant values in “codes”, would it be simpler to just set a range 0…506 to pull the cases or is that like inefficient because it would add more for the system to process? Thanks.


#9

Yes, that would be the way to go if you wanted to test all values in that range.