Being new to Swift this is what I found:
enum HttpMethod {
static let post = "POST"
static let get = "GET"
}
// can assign to string property.
request.httpMethod = HttpMethod.post // --> "POST"
The reason to use a caseless enum
and not a struct
makes sense to me after reading this but is not the thing I'm interested here.
Having a strong C# background this is how I would implement it:
enum HttpMethod: String {
case post = "POST"
case get = "GET"
}
// I'd even consider this alternatively:
enum HttpMethod: String {
case post
case get
}
// Must retrieve string value
request.httpMethod = HttpMethod.post.rawValue // --> "POST" or "post"
The second version requires the usage of rawValue
but it treats the enum as a real enum. Coming from C# I'm used to using .ToString()
on enum values.
Is this all just coming down to personal preference and Swift's conventions to use a caseless enum instead of actual cases + rawValue, or is there another (technical) reason to prefer the first over the second version?