Introduction
The Swift programming language is a powerful and versatile language that allows developers to create robust and efficient applications. One of the features of Swift is the ability to create enumerations, or enums, which are used to define a set of related values. However, when declaring an enum case, it is important to be aware of the syntax rules to avoid errors. In this article, we will discuss the “Extraneous ‘.’ in enum ‘case’ declaration” error and how to solve it. We will look at the causes of this error and the steps to take to resolve it. Finally, we will provide some tips to help you avoid this error in the future.
Solution
The error message is indicating that you have an extra period in your enum case declaration. To fix this, simply remove the extra period.
For example, if you have the following code:
enum MyEnum {
case foo.
case bar
}
You would need to remove the period after “foo” so that it looks like this:
enum MyEnum {
case foo
case bar
}
Swift enumeration cases are defined as case someName
, not case .someName
.
This is an easy syntax error when declaring a new enum’s cases, as in most other situations you will be typing .someName
via dot syntax. But when first declaring that enum case, it’s case someName
without the period.
enum SomeEnum {
case one
case two
var otherCase: Self {
switch self {
case .one: return .two
case .two: return .one
}
}
}
1
solved Swift enum: “Extraneous ‘.’ in enum ‘case’ declaration” [closed]
If you’re getting an error message that says “Extraneous ‘.’ in enum ‘case’ declaration” when trying to compile your Swift code, it’s likely because you’ve used a period (.) in the declaration of an enum case. This is not allowed in Swift, and the compiler will throw an error.
The correct syntax for declaring an enum case in Swift is to use the case keyword followed by the name of the case, without any punctuation. For example, if you wanted to declare an enum with two cases, you would write it like this:
enum MyEnum {
case firstCase
case secondCase
}
Notice that there are no periods in the case declarations. If you try to use a period, the compiler will throw an error.
If you’re getting this error, make sure to check your enum declarations and remove any periods that may have been accidentally added.