Get Associated Value from Enumeration Without Switch/Case

How to access a Swift enum associated value outside of a switch statement

As others have pointed out, this is now kind of possible in Swift 2:

import CoreGraphics

enum Line {
case Horizontal(CGFloat)
case Vertical(CGFloat)
}

let min = Line.Horizontal(0.0)
let mid = Line.Horizontal(0.5)
let max = Line.Horizontal(1.0)

func doToLine(line: Line) -> CGFloat? {
if case .Horizontal(let value) = line {
return value
}
return .None
}

doToLine(min) // prints 0
doToLine(mid) // prints 0.5
doToLine(max) // prints 1

Determine value of parameter in a generic enum without a switch

Don't forget there is the if case statement, which does pattern matching in an if-like style:

public var error: ResultError? {
if case .error(let e) = self {
return e
} else {
return nil
}
}

And there is nothing wrong with using a switch. I think it is perfectly fine and even more readable than if case.

swift enum get the associated value of multiple case with same parameters in single switch-case

You can put multiple enum values in the same case line, but you have to move the let into the ():

var value: Double {
switch self {
case .width(let value), .height(let value), .xxxxx1(let value):
return value
}
}

You might want to put each enum value on a separate line:

var value: Double {
switch self {
case .width(let value),
.height(let value),
.xxxxx1(let value):
return value
}
}

Unfortunately, that's about as elegant as it gets with enums with associated values. There's no way to get the associated value without explicitly listing the enums.

You can only combine values in the same line that have the same type of associated value, and all of the values have to bind to the same variable name. That way, in the code that follows, you know that the value is bound and what its type is no matter which enum pattern matched.

Extract value from enum using guard or if case in Swift

This is the correct syntax:

if case .one(value: let value) = self.model {
// do something with `value`
}

guard case .one(value: let value) = self.model else {
//handle case where self.model != .one
}
// do something with `value`

Can we use `if` or `guard` instead of `switch` on an enum to extract a value?

enum NetworkResult<T> {
case json(T)
case error(Error)

var error: Error? {
if case let .error(error) = self {
return error
} else {
return nil
}
}
}

guard case let .error(error) = self else {
return nil
}

return error

Swift Enum associated values conforming to single protocol

I've found the solution in case anyone will need this too.

enum ContentType {
case content1(Type1 & Refreshable)
case content2(Type2 & Refreshable)
case content3(someLabel: Type3 & Refreshable)

func refreshMe() {
let caseReflection = Mirror(reflecting: self).children.first!.value
(caseReflection as? Refreshable)?.refresh() //If associated type doesn't have label

let refreshable = Mirror(reflecting: caseReflection).children.first?.value as? Refreshable
refreshable?.refresh() //If associated type has label
}
}

Swift enum - switch statement matching associated values warning

Spell it like

case .entry0(let x, _):

Or like

case let .entry0(x, _):

which works more generally like:

case let .entry0(x, y):

Is it possible to write compound switch case between enum associated value conditional and another enum case?

You don't need a where-clause to match .cloudy(coverage: 0), just

case .cloudy(coverage: 0), .sunny: 
print("☀️")

Another option is to use fallthrough, for example

case .cloudy(let coverage) where coverage < 10:
fallthrough
case .sunny:
print("☀️")


Related Topics



Leave a reply



Submit