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.
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)
}
}
}
Dont know exactly why are XCUIElements needed but do something like this
//MARK: Declaration
enum CustomTextFieldTypes {
case CardType(String)
case CardNumber(String)
case CardExpiryDate(String)
case CardName(String)
case CCVNumber(String)
}
//MARK: Definition
var cardNumber = CustomTextFieldTypes.CardNumber("123")
var cardExpiry = CustomTextFieldTypes.CardExpiryDate("10-10-2016")
//MARK: Usage
func useCard(type: CustomTextFieldTypes)
{
switch type {
case .CardNumber(let numberString):
print(numberString)
case .CardType(let cardtype):
print(cardtype)
case .CardExpiryDate(let expiryDate):
print(expiryDate)
case .CardName(let name):
print(name)
case .CCVNumber(let ccvnumber):
print(ccvnumber)
}
}
useCard(cardNumber)
useCard(cardExpiry)
If you really neeed XCUIElement then change case CardType(String) to case CardType(String, XCUIElement) and update all the other code as well
Related
I'm kinda block for this scenario , I have a enum which have the same value now the question here is that they have different usecases how can I put a condition for this to case in switch:
supposed I have this:
enum ApiType: String, CaseIterable {
case dashboard = "dashboardRootIdApi"
case profile = "profileRootIdApi"
case usemeInLogin = "authenticationAccessIdApi"
case usemeInLogout = "authenticationAccessIdApi"
}
and from my usecases classes:
func authenticationDtoScreen(for: ApiType) -> UIControllerSharedScreen {
switch myType {
case .usemeInLogin: {
return UIControllerScreenConfiguration(
for: .usemeInLogin,
title: "Login"
)
}
case .usemeInLogout: {
return UIControllerScreenConfiguration(
for: .usemeInLogout,
title: "Logout"
)
}
}
}
I know .usemeInLogout will never be happend cause of this .usemeInLogin.
Those string values don't have to be the raw value of your enum. It can be a calulated property:
enum ApiType: CaseIterable {
case dashboard
case profile
case usemeInLogin
case usemeInLogout
var apiType: String {
switch self {
case .dashboard: return "dashboardRootIdApi"
case .profile: return "profileRootIdApi"
case .usemeInLogin, .usemeInLogout: return "authenticationAccessIdApi"
}
}
}
Its a bit hard without context, but I'd suggest using simple enum for use case differentiation:
enum MyType: String {
case usemeInLogin
case usemeInLogout
}
And if needed, have a map from this enum to String, like:
var map: [MyType:String] = [:]
map[.usemeInLogin] = "something"
map[.usemeInLogout] = "something"
As per documentation and this thread, among others, for an enum of Ints I can print the case name as a string by simply doing this:
enum TestEnum: Int {
case one
case two
case three
}
let testEnum = TestEnum.two
print(testEnum)
// prints "two"
Which works of course. But if I try to do the same thing with CKAccountStatus, it prints the name of the enum:
import CloudKit
let testStatus = CKAccountStatus.noAccount
print(testStatus)
// prints "CKAccountStatus"
CKAccountStatus is an enum of Ints, just like the test enum above:
public enum CKAccountStatus : Int {
case couldNotDetermine
case available
case restricted
case noAccount
}
What am I doing wrong and/or why is this happening?
Your TestEnum is a swift enum. CKAccountStatus could be Objective C enum.
You can achieve it by confirming the CustomStringConvertible protocol by adding:
extension CKAccountStatus: CustomStringConvertible {
public var description: String {
switch self {
case .noAccount:
return "noAccount"
case .available:
return "available"
case .restricted:
return "restricted"
case .couldNotDetermine:
return "couldNotDetermine"
}
}
}
let testStatus = CKAccountStatus.available
print(testStatus) // available
There is
var tableView: MyTableView?
tableView?.onGoToA = {
self.goToA()
}
tableView?.onGoToB = {
self.goToB()
}
tableView?.onGoToC = {
self.goToC()
}
are there better way for this case or better that just use delegate?
If you have limited possible cases, I would have single closure that takes a enum as an argument and will have a switch case to decide which method to trigger
If each event has a value associated with it you can exploit enums with associated values as well :)
Example:
enum EventType {
case toA
case toB
case toC
}
Declare your closure as
var routerBlock: ((EventType) -> ())? = nil
Finally have router block implemented as
tableView?.routerBlock = {[weak self] (eventType) in
switch eventType {
case .toA:
self?.gotoA()
case .toB:
self?.goToB()
case .toC:
self?.goToC()
}
}
You can use the similar approach with delegates as well. Rather than having 3 delegate methods you can have one method which takes EventType and have the same switch block to decide which method to trigger
Hope this helps
Basically, I want to set up a function that uses 'for' as a parameter for readability.
enum Genre {
case drama
case comedy
}
func setupTable(for: Genre) {
switch for {
case .drama: break
case .comedy: break
}
}
I set something like this up but when i try and use the switch for 'for' it comes up as a keyword and throws a compile error.
Cheers
When using a keyword as a normal identifier you have to escape it using backticks ` like this
func setupTable(for: Genre) {
switch `for` {
case .drama: break
case .comedy: break
}
}
On a side note, as pointed out by #Hamish in a comment on the question, you should try to avoid using such names for variables, which (in this case) you can do by providing both an internal and external name for the parameter:
func setupTable(for genre: Genre) {
switch genre {
case .drama: break
case .comedy: break
}
}
I need to convert an enum to a string, using this variable:
var bloodType:HKBloodTypeObject? = healthKitStore.bloodTypeWithError(&error);
And this enum:
enum HKBloodType : Int {
case NotSet
case APositive
case ANegative
case BPositive
case BNegative
case ABPositive
case ABNegative
case OPositive
case ONegative
}
I know that there are other questions similar to this, but I haven't found any answers that worked for me.
As simple description: the HKBloodTypeObject as wrapper of HKBloodType parameter that is in the HealthKit storage.
extension HKBloodTypeObject {
func string()->String {
switch self.bloodType {
case .abNegative:
return "AB-"
case .abPositive:
return "AB+"
case .aNegative:
return "A-"
case .aPositive:
return "A+"
case .bNegative:
return "B-"
case .bPositive:
return "B+"
case .oNegative:
return "O-"
case .oPositive:
return "O+"
default:
return "Not Set"
}
}
}
the best way to use this extension of HKBloodType enum.
Hope lines of code above 'll help you.
Create a en extension to HKBloodType that implements CustomStringConvertible (Printable for Swift < 2), see here: https://stackoverflow.com/a/24707744/335974