Junior — Middle
How to distinguish a data structure from an enumeration and what are their main differences?
sobes.tech AI
Answer from AI
Data structures (struct) and enumerations (enum) are different data types used for different purposes:
-
Structure (struct) — a data type that combines multiple values (fields) of different types into a single entity. It is useful when you need to store related data together. For example, a "Point" structure might contain x and y coordinates.
-
Enumeration (enum) — a data type that defines a set of named constants, usually associated with a limited number of options. Enumerations are convenient for representing states, options, or categories.
Example in Swift:
struct Point {
var x: Double
var y: Double
}
enum Direction {
case north
case south
case east
case west
}
Main difference: a structure stores data with different fields, while an enumeration is a choice among fixed options.