Keyword "Protocol" as function parametr [duplicate] - ios

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
}
}

Related

Swift 5 - Enum inside of enum

I'm trying to use enum to classify items. As such, I've ended up with having enums inside of enums. And I'm not entirely sure on how this whole enum thing works.
But what I want to do is tap into the enum MaterialClassification, and then if theres a second enum, like in case clay, tap into that enum's value to return it as a string.
enum MaterialClassification {
case clay(value: Clay)
case flux//(value: Fluxs)
case glassFormer
case stain//(value: Clay)
case accessoryMaterial
case all//(value: Clay)
}
extension MaterialClassification {
var materiaIdentifier: String {
switch self {
case .clay:
return "clay"
case .flux:
return "flux"
case .glassFormer:
return "glassFormer"
case .stain:
return "stain"
case .accessoryMaterial:
return "accessoryMaterial"
case .all:
return "all"
}
}
}
enum Clay {
case iskaolin// = "Kaolin"
case isPrimaryKaolin// = "Primary Kaolin"
case isSecondaryKaolin //= "Secondary Kaolin"
case isBallClay //= "Ball Clay"
case isStoneware// = "Stoneware"
case isFireClay //= "Fire Clay"
case isEarthenware// = "Earthenware"
case isVolcanicClay// = "Volcanic"
}
extension Clay {
var clayType: String {
switch self {
case .iskaolin:
return "Kaolin"
case .isPrimaryKaolin:
return "Primary Kaolin"
case .isSecondaryKaolin:
return "Secondary Kaolin"
case .isBallClay:
return "Ball Clay"
case .isStoneware:
return "Stoneware"
case .isFireClay:
return "Fire Clay"
case .isEarthenware:
return "Earthenware"
case .isVolcanicClay:
return "Volcanic Clay"
}
}
}
My goal is to be able to return the nested string when needed. For example:
materialClassification: MaterialClassification.clay(type: Clay.isPrimaryKaolin)
I need a way to return "Primary Kaolin". But I can't figure out how to connect the 2 enums.
If I understand your question correctly, you want to access associated types' properties. You can add a new property to your MaterialClassification enum and use it to access your cases.
Something like this should work
var type: String? {
switch self {
case .clay(let clay):
return clay.clayType
case .flux(let flux):
return flux.fluxType
case .stain(let stain):
return stain.stainType
case .glassFormer, .accessoryMaterial, .all:
return nil
}
}

Switch statement nested format - which is correct?

I often use nested cases and I wrap them with { }. My question is which of these are correct? Both seems to work fine.
switch (yourMom) {
case 1: {
// so fat
}
break;
default:
break;
}
OR
switch (yourMom) {
case 1: {
// so fat
break;
}
default:
break;
}
Both are equivalent.
The braces are merely defining scope, and break is not subject to scoping.
For what it's worth, I tend to use the first of your two cases; as it's a little clearer to the reader that the case labels do not follow through to each other.
(You'd need the braces if case 1 for example was declaring a variable.)

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.
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

Swift- access enumeration by type

I've got an enum in Swift. It's kind of like
enum LegalArgs {
case AsString(String)
case AsBool(Bool)
... etc
}
I want to access this enum conditionally by type. So if I have an instance of LegalArgs, I can pass T and get back a T? if the instance was of that type. Otherwise I will have to duplicate a bunch of code for different cases.
My current code looks a bit like this:
String? maybeAsString(arg: LegalArgs) {
switch arg {
case .AsString(let str):
return str;
default:
return nil;
}
}
The problem is that I've got to duplicate this function for every case in the enum.
You can use a generic asType function:
enum LegalArgs {
case AsString(String)
case AsBool(Bool)
case AsNumber(Int)
func asType<T>(type: T.Type) -> T? {
switch self {
case AsString(let str): return str as? T
case AsBool(let bol): return bol as? T
case AsNumber(let num): return num as? T
}
}
}
// usage
LegalArgs.AsBool(true).asType(Bool.self) // true
LegalArgs.AsBool(true).asType(String.self) // nil

How to convert HKBloodType Enum to a string

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

Resources