Accessing an Enumeration association value in Swift

Swift

Swift Problem Overview


In this code I've written a really useless enum that defines a possible Number with Int or Float.

I can't understand how can I access the value that I set with the association. If I try to print it I get just (Enum Value)

enum Number {
    case int (Int)
    case float (Float)
}

let integer = Number.int(10)
let float = Number.float(10.5)
println("integer is \(integer)")
println("float is \(float)")

Swift Solutions


Solution 1 - Swift

For sake of completeness, enum's association value could be accesed also using if statement with pattern matching. Here is solution for original code:

enum Number {
  case int (Int)
  case float (Float)
}

let integer = Number.int(10)
let float = Number.float(10.5)

if case let .int(i) = integer {
  print("integer is \(i)")
}
if case let .float(f) = float {
  print("float is \(f)")
}

This solution is described in detail in: https://appventure.me/2015/10/17/advanced-practical-enum-examples/

Solution 2 - Swift

The value is associated to an instance of the enumeration. Therefore, to access it without a switch, you need to make a getter and make it available explicitly. Something like below:

enum Number {
    case int(Int)
    case float(Float)
    
    func get() -> NSNumber {
        switch self {
        case .int(let num):
            return num
        case .float(let num):
            return num
        }
    }
}

var vInteger = Number.int(10)
var vFloat = Number.float(10.5)

println(vInteger.get())
println(vFloat.get())

Maybe in the future something like that may be automatically created or a shorter convenience could be added to the language.

Solution 3 - Swift

It surprises me that Swift 2 (as of beta 2) does not address this. Here's an example of a workaround approach for now:

enum TestAssociatedValue {
  case One(Int)
  case Two(String)
  case Three(AnyObject)
  
  func associatedValue() -> Any {
    switch self {
    case .One(let value):
      return value
    case .Two(let value):
      return value
    case .Three(let value):
      return value
    }
  }
}

let one = TestAssociatedValue.One(1)
let oneValue = one.associatedValue() // 1
let two = TestAssociatedValue.Two("two")
let twoValue = two.associatedValue() // two

class ThreeClass {
  let someValue = "Hello world!"
}

let three = TestMixed.Three(ThreeClass())
let threeValue = three. associatedValue() as! ThreeClass
print(threeValue.someValue)

If your enum mixes cases with and without associated values, you'll need to make the return type an optional. You could also return literals for some cases (that do not have associated values), mimicking raw-value typed enums. And you could even return the enum value itself for non-associated, non-raw-type cases. For example:

enum TestMixed {
  case One(Int)
  case Two(String)
  case Three(AnyObject)
  case Four
  case Five
  
  func value() -> Any? {
    switch self {
    case .One(let value):
      return value
    case .Two(let value):
      return value
    case .Three(let value):
      return value
    case .Four:
      return 4
    case .Five:
      return TestMixed.Five
    }
  }
}

let one = TestMixed.One(1)
let oneValue = one.value() // 1
let two = TestMixed.Two("two")
let twoValue = two.value() // two

class ThreeClass {
  let someValue = "Hello world!"
}

let three = TestMixed.Three(ThreeClass())
let threeValue = three.value() as! ThreeClass
print(threeValue.someValue)

let four = TestMixed.Four
let fourValue = four.value() // 4

let five = TestMixed.Five
let fiveValue = five.value() as! TestMixed

switch fiveValue {
case TestMixed.Five:
  print("It is")
default:
  print("It's not")
}
// Prints "It is"

Solution 4 - Swift

like @iQ. answer, you can use property in enum also

enum Number {
    case int (Int)
    var value: Int {
        switch self {
            case .int(let value):
                return value
        }
    }
}

let integer = Number.int(10)
println("integer is \(integer.value)")

Solution 5 - Swift

I have used something like this:

switch number {
case .int(let n):
    println("integer is \(n)")
case .float(let n):
    println("float is \(n)")
}

Solution 6 - Swift

If you're using guard, you can write like below:

enum Action {
	case .moveTab(index: Int)
}

guard let case .moveTab(index) = someAction else { return }

Solution 7 - Swift

You can access enum associated value not only through switch! Mirrors come to our aid

Let's create a protocol

protocol MirrorAssociated {
    var associatedValues: [String: Any] { get }
}

extension MirrorAssociated {
    var associatedValues: [String: Any] {
        var values = [String: Any]()
        if let associated = Mirror(reflecting: self).children.first {
            let children = Mirror(reflecting: associated.value).children
            for case let item in children {
                if let label = item.label {
                    values[label] = item.value
                }
            }
        }
        return values
    }
}

and use it like this:

enum Test: MirrorAssociated {
    case test(value: String, anotherValue: Int)
}

Now we can access any associated value without using switch:

let test: Test = .test(value: "Test String", anotherValue: 1337)

if let value = test.associatedValues["value"] as? String {
    print("\(value)") // "Test String"
}

if let intValue = test.associatedValues["anotherValue"] as? Int {
    print("\(intValue)") // 1337
}

Solution 8 - Swift

Swift 5

enum Directory {
    case accountImages(URL)
    case accountData(URL)
    
    var url: URL {
        switch self {
        case .accountImages(let url):
            return url
        case .accountData(let url):
            return url
        }
    }
}

func save(to directory: Directory) {
    let dir = directory.url
}

Solution 9 - Swift

Swift 4,

I have created a simple enum with associated values for handling firebase database reference paths

import Firebase

    enum FirebaseDBConstants  {
    
        case UserLocation(database : DatabaseReference, userID :String)
        case UserRequest(database : DatabaseReference, requestID :String)
    
        func getDBPath() -> DatabaseReference {
            switch self {
            case  .UserLocation(let database,let userID):
                return database.root.child(FirebaseDBEnvironmentEnum.getCurrentEnvioronMent()).child("Location").child(userID).child("JSON")
    
            case .UserRequest(let database,let requestID):
                return database.root.child(FirebaseDBEnvironmentEnum.getCurrentEnvioronMent()).child("Request").child(requestID)
    
            default:
                break
            }
        }
    }

Use it like as shown

//Pass Database refenence root as parameter with your request id
let dbPath = FirebaseDBConstants.UserRequest(database: database, requestID: requestId).getDBPath()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatterGoalView Question on Stackoverflow
Solution 1 - SwiftMarek GregorView Answer on Stackoverflow
Solution 2 - SwiftiQ.View Answer on Stackoverflow
Solution 3 - SwiftScott GardnerView Answer on Stackoverflow
Solution 4 - SwiftZoon NoozView Answer on Stackoverflow
Solution 5 - SwiftmanojldsView Answer on Stackoverflow
Solution 6 - SwiftBurtKView Answer on Stackoverflow
Solution 7 - SwiftIlia KambarovView Answer on Stackoverflow
Solution 8 - SwiftliquidView Answer on Stackoverflow
Solution 9 - SwiftPramod MoreView Answer on Stackoverflow