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.
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.
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.
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.
A Swift extension allows you to add functionality to a type, a class, a struct, an enum, or a protocol.
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
}
}
}
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With