1

I'm learning Swift 2

I have this code:

case .Login(let parameters):
    return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .GetUpComingRides(let parameters):
    return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0

There is a lot of redundancy. I have tried this:

case .Login(let parameters), .GetUpComingRides(let parameters):
    return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0

But I'm getting the warning of immutable value 'parameters' was never used;

How do I check for multiple case statements and get the values?

I feel that I'm missing something simple here.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
oky_sabeni
  • 7,672
  • 15
  • 65
  • 89
  • 3
    Does this answer your question? [Can I bind different associated values in a Swift enum to the same var?](https://stackoverflow.com/questions/32827507/can-i-bind-different-associated-values-in-a-swift-enum-to-the-same-var) – 0-1 Jul 15 '20 at 17:23

1 Answers1

0

In newer versions of Swift, this is now possible if the values are of the same type:

enum Foo {
    case bar(Int),  bas(Int)
}

var this = .random() ? Foo.bar(5) : Foo.bas(5)
switch this {
    case .bar(let foo), .bas(let foo): print(foo) // Always prints 5
}
0-1
  • 702
  • 1
  • 10
  • 30