How to create a predicate to filter array of enums with associated values in Swift? How to create a predicate to filter array of enums with associated values in Swift? arrays arrays

How to create a predicate to filter array of enums with associated values in Swift?


The filter function can either be invoked as a global function over an array, or as an instance method (I prefer the later as it is more OO).

It accepts a closure with one parameter (the element being evaluated) that return a boolean (indicating whether the element matches the required condition).

Since it's a simple closure in a clear situation it's ok to use the abbreviated form.

I guess that other "With" cases would be added to your enum, so you could go with something like:

let filteredArray = enums.filter {     switch $0 {      case let .WithString(value):        return value == "C"      default:        return false    } }

That should do the trick in your example.


As someone already mentioned, for Swift > 2.0 there's if case statement available:

enums.filter {  if case .WithString("C") = $0 {    return true  }  return false}

But, it doesn't look nice, especially if you are going to repeat same logic again. What we can do here is to make EnumType conform to Equatable:

extension EnumType: Equatable {}func ==(lhs: EnumType, rhs: EnumType) -> Bool {    switch (lhs, rhs) {    case (.WithString(let lStr), .WithString(let rStr)):        return lStr == rStr    }}

And now you can just:

enums.filter { $0 == .WithString("C") }


You could try adding a simple extension with a computed property to your enum and filter to that property:

extension EnumType {  var isC: Bool {    switch self {    case .WithString(let message): return message == "C"    default: return false    }  }}

After this, you could simpy use the filtering as usual:

enums.filter { $0.isC }