Swift Constants: Struct or Enum

Swift constants: Struct or Enum

Both structs and enumerations work. As an example, both

struct PhysicalConstants {
static let speedOfLight = 299_792_458
// ...
}

and

enum PhysicalConstants {
static let speedOfLight = 299_792_458
// ...
}

work and define a static property PhysicalConstants.speedOfLight.

Re: A struct will be copied every time i use it or not?

Both struct and enum are value types so that would apply to enumerations as well. But that is irrelevant here
because you don't have to create a value at all:
Static properties (also called type properties) are properties of the type itself, not of an instance of that type.

Re: What advantages has the choice of a struct or enum?

As mentioned in the linked-to article:

The advantage of using a case-less enumeration is that it can't accidentally be instantiated and works as a pure namespace.

So for a structure,

let foo = PhysicalConstants()

creates a (useless) value of type PhysicalConstants, but
for a case-less enumeration it fails to compile:

let foo = PhysicalConstants()
// error: 'PhysicalConstants' cannot be constructed because it has no accessible initializers

Confused by the usage of a class or a struct as container for static variables (Swift)

What you're doing is clumping global constants into a namespace.

Is there a special Container Object in Swift designed for such purposes?

Yes, a caseless enum is the conventional way to do this, as it is the most lightweight and cannot be accidentally instantiated; it is "pure" namespace.

If you watch some Apple videos you'll see that's how they do it. Personally I used to recommend a struct but I've switched to using enums for the reason given.

Swift Constant file?

Class and singleton class are designed to serve different purpose. Those should be excluded when we are talking about writing pure constants. Among two left options are struct and enum. Among them enum usually used when we group similar type of constants. For example we may keep possible states of a variable in enum. Other option struct is the best to use in your case as well as in othe cases where we deal with pure constants.

Static properties in enum

Why shouldn't you be able to do this?

Swift enums are first-class types, just like structs and classes. Swift enums 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

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

How to use nested constants in Swift

Operator overloading may help you to achieve similar functionality.
Please look at my solution, you can extend it a lot more

enum Vehicle: String {
case car = "Car"
case airplane = "Airplane"

enum CarPart: String {
case door = "Door"
case window = "Window"
}
}

func > (left: Vehicle, right: Vehicle.CarPart) -> String {
return left.rawValue + right.rawValue
}


let string = .car > .window // "CarWindow"

Struct , class or enum for service object with static methods?

There are two key difference between a class and a struct in Swift. A class allows for inheritance and a class is a reference type while a struct is a value type.

Make your decision based on those few differences.

Since everything in Linker is static, the difference between reference and value becomes irrelevant since you won't have any instances.

So that leaves inheritance. Will you ever subclass Linker? If not, use a struct. If yes, use a class.

And now that you are asking about enum, you can probably rule that out over struct since Linker doesn't appear to have any constants, just methods.



Related Topics



Leave a reply



Submit