skip to Main Content

I am using Enum in swift I am looking to pass dynamic value, but not sure how to achieve it ?

I want to pass dynamic value in .revisit case

Here is my code example: –

enum MyEnum: String, CaseIterable {
    case revision
    case revisit
  
    var getDescription: String {
        switch self {
        case .revision:
            return "Do revision"
        case .revisit:
            return "You answered 3 Out Of 3 Questions re-start ?" // How to pass dynamic value instead of 3 & 3
        }
    }
}

By using CaseIterable I am using it on UITableViewCell & want to change data of revisit dynamically

2

Answers


  1. Try this:

    var i:Int = 2;
    
    enum MyEnum: String, CaseIterable {
        case revision
        case revisit
      
        var getDescription: String {
            switch self {
            case .revision:
                return "Do revision"
            case .revisit:
                return "You answered (i) Out Of 3 Questions re-start ?" // How to pass dynamic value instead of 3 & 3
            }
        }
    }
    
    Login or Signup to reply.
  2. Better way make pass dynamic value in enum

      enum MyEnum: String, CaseIterable {
        case revision
        case revisit
    
        private var dynamicValues: (Int, Int)? {
            switch self {
            case .revision:
                return nil
            case .revisit:
                return (3, 3) // Default values
            }
        }
    
        var getDescription: String {
            switch self {
            case .revision:
                return "Do revision"
            case .revisit:
                if let (value1, value2) = dynamicValues {
                    return "You answered (value1) Out Of (value2) Questions re-start ?"
                } else {
                    return "You answered some questions. Re-start?"
                }
            }
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search