Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift enum function for only a single case of the enum?

I want to declare a function which I can only use for a single specific enum case.

For example I have CustomTextFieldTypes enum. This has the following cases and functions.

enum CustomTextFieldTypes {

    case CardType
    case CardNumber
    case CardExpiryDate
    case CardName
    case CCVNumber

    func inputCardNumber(cardNumber: String!, cardNumberTextField: XCUIElement?) {        
       cardNumberTextField?.typeText(cardNumber)
    }

    func inputCardCCVNumber(cardCCVNumber: String!, cardCCVNumberTextField: XCUIElement?) {        
       cardCCVNumberTextField?.typeText(cardCCVNumber)
    }
}

Now I want to call the inputCardNumber(...) function only for the CustomTextFieldTypes.CardNumber case. I can do the following...

CustomTextFieldTypes.CardNumber.inputCardNumber(...) 

But at the same time I can do this...

CustomTextFieldTypes.CardExpiryDate.inputCardNumber(...) or    
CustomTextFieldTypes.CardNumber.inputCardNumber(...) 

I only want to call the inputCardNumber(...) function for the CardNumber case. Not from another case the enum itself. How do I achieve this?

Thanks in advance for any help

EDIT- Here's some background on what I'm doing. I was writing a UI test which would input text into text fields. I wanted to keep the input code away from my test file and I started "Experimenting" with enums and enum functions. I was wondering if I could have a function explicitly available for an enum case. Judging from the comments I cannot do this (I checked online but didn't get far). It's not a bad architecture or anything, I was just splitting up test code..

Thanks for everyone for replying.

like image 581
harmeet07 Avatar asked Aug 19 '16 17:08

harmeet07


People also ask

Can enum have functions in Swift?

Enumerations (or enums for short) in Swift define a common type for a group of related values. According to the Swift documentation, enums enable you to work with those values in a type-safe way within your code. Enums come in particularly handy when you have a lot of different options you want to encode.

What is enum or enumerations in Swift?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.

When declaring an enumeration multiple member values can appear on a single line separated by Which punctuation mark?

In Visual Basic, the separator character is the colon ( : ). Use separators when you want to include multiple statements on a single line instead of separate lines. This saves space and improves the readability of your code. The following example shows three statements separated by colons.

Can we extend enum in Swift?

A Swift extension allows you to add functionality to a type, a class, a struct, an enum, or a protocol.


2 Answers

You can perform a switch on self in order to execute certain code for certain cases. Here's an example:

enum CustomTextFieldTypes {

    case cardType
    case cardNumber
    case cardExpiryDate
    case cardName
    case ccvNumber

    func inputCardNumber(cardNumber: String!, cardNumberTextField: XCUIElement?) {  
        switch self {
        case .cardNumber:
            cardNumberTextField?.typeText(cardNumber)
        default:
            return
        }
    }
}
like image 127
Aaron Avatar answered Sep 29 '22 11:09

Aaron


No need to use a switch when you only want to match a single case:

enum CustomTextFieldTypes {

  case cardType
  case cardNumber
  case cardExpiryDate
  case cardName
  case ccvNumber

  func inputCardNumber(cardNumber: String!, cardNumberTextField: XCUIElement?) {
    if case .cardNumber = self {
        cardNumberTextField?.typeText(cardNumber)
    }
}

}

like image 26
altyus Avatar answered Sep 29 '22 12:09

altyus