Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift constants: Struct or Enum

I'm not sure which of both are better to define constants. A struct or a enum. A struct will be copied every time i use it or not? When i think about a struct with static let constants it makes no sense that it will copied all the time, in my opinion. But if it won't copied then it doesn't matter what I take?

What advantages does the choice of a struct or enum?

Francisco say use Struct's.

Ray Wenderlich say use Enum's. But I lack the justification.

like image 906
Paixsn Avatar asked Jul 26 '16 08:07

Paixsn


People also ask

When to use enum vs struct Swift?

One difference between the two is that structs can be instantiated where as enums cannot. So in most scenarios where you just need to use constants, it's probably best to use enums to avoid confusion. The above code would still be valid.

What is the difference between enum and struct?

Enum is to define a collection of options available. A struct can contain both data variables and methods. Enum can only contain data types. A struct supports a private but not protected access specifier.

Can enums have methods Swift?

Swift's Enum can have methods. It can have instance methods and you can use it to return expression value for the UI. Let's look at the code above.


3 Answers

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
like image 84
Martin R Avatar answered Oct 19 '22 11:10

Martin R


Here's a short answer: Do your constants need to be unique? Then use an enum, which enforces this.

Want to use several different constants to contain the same value (often useful for clarity)? Then use a struct, which allows this.

like image 24
jglasse Avatar answered Oct 19 '22 11:10

jglasse


One difference between the two is that structs can be instantiated where as enums cannot. So in most scenarios where you just need to use constants, it's probably best to use enums to avoid confusion.

For example:

struct Constants {
    static let someValue = "someValue"
}

let _ = Constants()

The above code would still be valid.

If we use an enum:

enum Constants {
    static let someValue = "someValue"
}

let _ = Constants() // error

The above code will be invalid and therefor avoid confusion.

like image 14
SilentK Avatar answered Oct 19 '22 13:10

SilentK