Working with a Function inside a Swift Enumeration

By Jesse Feiler

With the same basic code, you can add a function to an enumeration in Swift. This is something you haven’t seen in C and perhaps not in other languages either. It’s a new way of looking at enumerations.

Adding a simple function to an enumeration

Here is the function that’s added to the enumeration. You can place it anywhere (except, of course, not in the middle of the case):

  func enumFunction () -> Int {
    return -17
  }

The function is named enumFunction and it returns an Int with the value of –17. You can access this function from any instance of the enumeration. Here’s one example:

let i = Place.bars.enumFunction()

You may be thinking, “Wait a moment. That enumeration is typed as String and so it must return a String.”

That’s true, but you’re not referring to the value returned by the enumeration. You are asking a member of the enumeration (not even an instance of the enumeration) to return the result of enumFunction, which is declared as Int.

Adding a switch statement to a function inside an enumeration

Swift enumerations are often used in conjunction with switch statements. Here is a function to place inside the enumeration that contains a switch statement. When you have an instance of an enumeration (or just the enumeration itself), you can call an internal function from an element just as you saw in the preceding code snippet. And, like the preceding code snippet, the result returned by the function need not be the same type as the type of the enumeration.

In the following code a function that returns a String is based on the result of a switch statement that uses the elements of the enumeration. Note that the elements of the switch are not strings (note the absence of quotation marks and the presence of the leading periods in the case names).

Experiment with this code, and you’ll see that if you set the type of the enumeration to Int (or Double or anything other than String) the function will still return a String. That’s the result of the function and not the type of the enumeration. This is a very useful technique to use, and many developers find that it dramatically reduces the code they have to write. (Those if-then-else-if statements inside switch case statements can be omitted, as can switch statements within case statements.)

func enumChoiceFunction () -> String {
    switch self {
    case .track1, track2:
      return "running or walking"
    case .park:
      return "Walking, sitting on a bench, feeding birds"
    default:
      return "enjoying nature"
    }
  }

The figure shows the code described here.

image0.jpg

The code itself is provided here:

image1.jpg

enum Place: String {
  case
    park = "park",
    pool = "swimming pool",
    bars = "climbing bars",
    track1 = "running track",
    track2 = "walking track"
  static let facilities = [park, pool, bars, track1, track2]
  func enumFunction () -> Int {
    return -17
  }
  func enumChoiceFunction () -> String {
    switch self {
    case .track1, track2:
      return "running or walking"
    case .park:
      return "Walking, sitting on a bench, feeding birds"
    default:
      return "enjoying nature"
    }
  }
}
let i = Place.bars.enumFunction()
let e = Place.pool.rawValue
println (Place.pool)
let x = Place.track1.enumChoiceFunction()
var result: String = "
for amenity in Place.facilities {
  result = result + amenity.rawValue + ", "
}
println (result)