How to access a passed argument Enum in Swift 3

I am having trouble accessing a parameter that is passed with an enum.

The generic RequestType will be included.

enum RequestType {
  case flagging(api : FlaggingRequestType)
}

      

Here is my enum that takes another FlaggingRequestType (another enum that takes a string parameter)

enum FlaggingRequestType {
  case getFlag(api : String)
}

protocol Requestable {
  var requestType : RequestType { get set }
}

      

This is where I build my flag

let flaggingRequest = RequestBuilder.buildFlaggingRequest(flagRequest: .getFlag(api: "http://www.apiworld.com"))

      

Here is my method for actually sending the request using another method.

func sendRequest(for apiRequest : Requestable) {
  switch apiRequest.requestType {
  case .flagging:
  self.flaggingAPI(for: apiRequest)
 }
}

      

The problem is I can't figure out how to access the value passed in the parameter api

found in apiRequest

/ flaggingRequest

. Is it possible? Hope it's clear :)

+3


source to share


3 answers


Here's a great link for enums with bound values https://appventure.me/2015/10/17/advanced-practical-enum-examples/#sec-1-5



func sendRequest(for apiRequest : Requestable) {
    switch apiRequest.requestType {
    case .flagging(let api):
        // access api value here
        self.flaggingAPI(for: apiRequest)
    }
}

      

+3


source


Nested pattern matching and value binding: case .one(.two(let val))

If, for a given instance of the type that matches Requestable

, your only goal is to access the bound value String

for the inner instance FlaggingRequestType

(for the known inner case

), you can use nested matching and pattern binding in one case

, for example used not only in a statement switch

, say if

.

// your example setup
enum FlaggingRequestType {
    case getFlag(api : String)
}

protocol Requestable {
    var requestType : RequestType { get set }
}

enum RequestType {
    case flagging(api : FlaggingRequestType)
}

// dummy type conforming to 'Requestable'
struct Foo : Requestable {
    var requestType: RequestType =
        .flagging(api: .getFlag(api: "http://www.apiworld.com"))
}

// Q: how to find the nested "http://www.apiworld.com" String
//    from a 'Foo' instance?
let foo = Foo()

// A: use nested pattern matching, e.g. in a single 'if' statement
if case .flagging(.getFlag(let api)) = foo.requestType {
    print(api) // http://www.apiworld.com
}

      

You can, of course, use nested pattern matching also in statement cases switch

:

switch foo.requestType {
case .flagging(.getFlag(let api)): print(api) // http://www.apiworld.com
// ...
default : () // in non-exhaustive pattern matching above
}

      

If a bound value of different types FlaggingRequestType

is of the same type and also has the same expected usage, this nested binding can be a useful alternative to multiple nested statements switch

. For example, if the extension of the example set above should be:



enum FlaggingRequestType {
    case getFlag(api : String)
    case getHandle(api : String)
    case getId(id: Int)
}

      

We could apply nested pattern matching in a single expression switch

, where a single one case

could cover related related values ​​for several different cases of internal enumeration, given that these related values ​​are of the same type.

switch foo.requestType {
case .flagging(.getFlag(let api)),
     .flagging(.getHandle(let api)): print(api) // api is 'String', a flag or a handle
case .flagging(.getId(let id)):      print(id)  // id is 'Int'
// ...
default : () // in case of non-exhaustive pattern matching above
}

      


Finally, note that you may want to use extension generated types for your method sendRequest

, not for the protocol input to be used as the type.

func sendRequest<T: Requestable>(for apiRequest : T) { /* ... */ }

      

+2


source


Since it is nested, you will have to do nested switch cases, for example:

switch apiRequest {
case .flagging(let value):
    switch value {
    case .getFlag(let api):
        print(api) // api is the string passed in the associated value
        break
    }
    break
}

      

+1


source







All Articles