Why do enums have computed properties but not stored properties in Swift?
enum
s do have stored type properties - i.e., static
properties. They don't have stored instance properties. I don't know if there is a technical reason why stored instance properties are not available for enum
s. You may have to ask your question on the dev forum if you want a technical answer for "why".
In your question you ask if associated values work like stored properties. In fact, they do, and are more flexible (in some ways) than stored properties for struct
s and class
es. Each case
in an enum
can have its own specialized set of data that is associated with it. Rather than have one set of stored properties that apply to all case
s, you get to individualize the stored properties for each case
.
Enum conforming to protocol with stored property
OK, so the error you are getting is because all properties must be computed. So, in your simple example, you could do something like this:
enum SimpleEnum: ExampleProtocol {
case foo, bar
var simpleDescription: String {
switch self {
case .foo: return "Foo"
case .bar: return "Bar"
}
}
func adjust() {
}
}
Creating enums with specific values/stored properties
Swift does not support stored properties in enums
You could make xAlter
and yAlter
computed properties. Here's a highly simplified example (only two cardinal directions):
enum CompassDirection {
case n
case s
var alterX : Int {
switch self {
case .n: return 0
case .s: return 0
}
}
var alterY : Int {
switch self {
case .n: return 1
case .s: return -1
}
}
}
Now you can add a method referring to self.alterX
and self.alterY
, exactly as you desire.
Static properties in enum
Why shouldn't you be able to do this?
Swift enums are first-class types, just like struct
s and class
es. Swift enum
s do not need to have cases, they can be completely empty types, just like how a struct
or class
does not need to have any properties.
enum Empty {} // completely valid
enum
s cannot have _ stored instance properties_, but they can have type properties (which static
properties are) and computed instance properties.
Caseless enums with static properties are often used for storing constant values. For more information on the topic, see Swift constants struct or enum
This might not be documented in the Enumerations section of the Swift docs, but nothing says that this shouldn't be possible either. On the other hand, the docs do state that enums are first-class types and there is a non-exhaustive list of features that enums share with classes and structs.
Why Swift Enum generate object, internal variable can't be altered?
You have made your MyEnum var some
a computed property. A computed property gets evaluated every time you read its value. Thus, every time you invoke x?.some
, you get a new Some
object.
As Rob pointed out, my initial suggestion for a way to fix this won't work. Enums can't have stored properites. I think you'll have to use an associated value on your enum case to do what you want.
Why no stored type properties for classes in swift?
The compiler error is "Class variables not yet supported" so it seems like they just haven't implemented it yet.
Related Topics
How to Use an Nsattributedstring with a Scrollview in Swiftui
Select() from Linq in Swift 3.0
Convert Avaudiopcmbuffer to Nsdata and Back
Hide/Show Tab Bar When Push/Back. Swift
In Swift 3, What Is a Way to Compare Two Closures
Swift: Print Decimal Precision of Division
In Xcode 6.1. 'Uiimage' Does Not Have a Member Named 'Size' Error
How to Stop Timer in Text View
Navigationview Pops Back to Root, Omitting Intermediate View
How to Make the Scroll of a Tableview Inside Scrollview Behave Naturally
How to Replicate Promisekit-Style Chained Async Flow Using Combine + Swift
How to Draw Text in PDF Context in Swift
Swift Extension: Same Extension Function in Two Modules