Use reserved keyword a enum case
From the Swift Language Guide (Naming Constants & Variables section)
If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.
enum MyEnum {
case `Self`
case AnotherCase
}
and use it with or without backticks
let x: MyEnum = .Self
let y = MyEnum.`Self`
How to declare and init nested enum with reserved keyword as type name in Swift?
There is no way to do this specific thing you want to do. That's why nobody uses nested types named Type
, even though we all want to—the language already provides this type, and you don't get to override it with your own. We all use the Objective-C style naming of just smashing the word Type right up there without a proper delimiter.
FooType
is what you've got to work with.
Is there a way to use a keyword as identifier in an enum?
No they cant be used.
From MSDN
Keywords are predefined reserved identifiers that have special
meanings. They cannot be used as identifiers in your program.
The rule for identifier says:
An identifier can be used to name objects, references, functions,
enumerators, types, class members, namespaces, templates, template
specializations, parameter packs, goto labels, and other entities,
with the following exceptions:
- the identifiers that are keywords cannot be used for other purposes;
- the identifiers with a double underscore anywhere are reserved;
- the identifiers that begin with an underscore followed by an uppercase letter are reserved;
- the identifiers that begin with an underscore are reserved in the global namespace.
Why enum is a reserved keyword in vanilla javascript?
According to the docs
The following are reserved as future keywords by the ECMAScript specification. They have no special functionality at present, but they might at some future time, so they cannot be used as identifiers.
And enum
is always reserved keyword.
Using reserved words in an enum / switch statement, best workaround?
What you're doing right now is the preferred way to do it. I would be extraordinarily shocked if the toUpperCase
were a bottleneck.
That said, I might consider something like
enum ParseTags {
CODE {
public void set(Entry entry, String value) {
entry.setCode(value);
}
},
...;
public abstract void set(Entry entry, String value);
}
so you can do
ParseTags.valueOf(token.toUpperCase()).set(entry, value);
Reserved Keyword in Enumeration in C#
Prefixing reserved words in C# is done with @.
public enum Test
{
@as = 1,
@is = 2
}
Related Topics
Swift 5: What's 'Escaping Closure Captures Mutating 'Self' Parameter' and How to Fix It
Adding 3D Object to Argeoanchor
Navigationview Pops Back to Root, Omitting Intermediate View
Why Non Optional Any Can Hold Nil
Class 'Viewcontroller' Has No Initializers in Swift
Perform Assignment Only If Right Side Is Not Nil
Iboutlet of Another View Controller Is Nil
How to Print Out the Method Name and Line Number in Swift
How to Save a Struct to Realm in Swift
Use Reserved Keyword a Enum Case
Swift Cross Compile to Single Linux Binary
Fixing Nsurlconnection Deprecation from Swift 1.2 to 2.0
How to Add Alamofire Url Parameters
Intrinsiccontentsize() - Method Does Not Override Any Method from Its Superclass
Handling Multiple Gesturerecognizers
Returning a Value from a Function with Alamofire and Swiftyjson
Does Swift Implement Tail Call Optimization? and in Mutual Recursion Case