Swift Basics: A Quick Comparison of Class, Struct, and Enum
🏛️ CLASS
In Swift, we generally use the term instance instead of object. In other words, an instance of a class type is not strictly referred to as an object.
Supports only single inheritance.
Has (Instance/Type) Methods and (Instance/Type) Properties (same as Structs).
It is a Reference Type.
Most of the traditional iOS frameworks (like UIKit) are heavily composed of classes.
(Note: In contrast, SwiftUI is predominantly composed of Structs.)
In Swift, we generally use the term instance instead of object. In other words, an instance of a class type is not strictly referred to as an object.
Supports only single inheritance.
Has (Instance/Type) Methods and (Instance/Type) Properties (same as Structs).
It is a Reference Type.
Most of the traditional iOS frameworks (like UIKit) are heavily composed of classes.
(Note: In contrast, SwiftUI is predominantly composed of Structs.)
🧱 STRUCT
Unlike Classes, inheritance is not possible.
Has (Instance/Type) Methods and (Instance/Type) Properties (same as Classes).
It is a Value Type.
The core backbone of Swift's standard library is mostly made up of Structs.
Basic data types like Int, Double, and String are actually Structs under the hood.
Unlike Classes, inheritance is not possible.
Has (Instance/Type) Methods and (Instance/Type) Properties (same as Classes).
It is a Value Type.
The core backbone of Swift's standard library is mostly made up of Structs.
Basic data types like Int, Double, and String are actually Structs under the hood.
💡 When to use Structs:
When you want to encapsulate a few related data values into a single compound type.
When you want the encapsulated data to be copied rather than referenced when passed to another function or assigned to a new variable.
When you don't need the type to inherit from another existing type, or to be inherited by other types.
(While Apple's older frameworks heavily rely on Classes, Structs are recommended by default in modern Swift unless you specifically need Class features).
(Click here to learn more about Structs)
📜 ENUM (Enumeration)
Inheritance is not possible.
Has (Instance/Type) Methods and (Instance/Type) Computed Properties.
Swiftenum Animal {
case tiger
case lion
case bear
}
let tiger2: Animal = Animal.tiger
let lion2: Animal = .lion
Inheritance is not possible.
Has (Instance/Type) Methods and (Instance/Type) Computed Properties.
enum Animal {
case tiger
case lion
case bear
}
let tiger2: Animal = Animal.tiger
let lion2: Animal = .lion
We defined animal names using an enumeration. The variable tiger2 was explicitly given the Animal type and assigned the value Animal.tiger.
However, for lion2, we declared it as an Animal type but only assigned .lion. This functions exactly the same way. When you declare the variable, the compiler already recognizes it as an Animal type, allowing you to use the shorthand dot syntax.
In Enums, the underlying value is called a rawValue. Let's assign a specific type to the Animal enum.
enum Animal: Int {
case tiger = 0
case lion
case bear
}
We assigned a raw value of 0 to tiger.
print(tiger2.rawValue) // 0
print(lion2.rawValue) // 1
print(bear2.rawValue) // 2
(If you use a Double type instead):
enum Animal: Double {
case tiger = 0
case lion
case bear
}
print(tiger2.rawValue) // 0.0
print(lion2.rawValue) // 1.0 (Note: Swift automatically increments by 1)
print(bear2.rawValue) // 2.0
✨ Various Ways to Use Enums
Swiftenum School {
case elementary
case middle
case high
// You can also define them on a single line like this:
// case elementary, middle, high
}
let yourSchool = School.elementary
print("YourSchool : \(yourSchool)") // YourSchool : elementary
enum Grade: Int {
case first = 1
case second = 2
}
let yourGrade = Grade.second
print("YourGrade : \(yourGrade.rawValue)") // YourGrade : 2
// Enum with Associated Values
enum SchoolDetail {
case elementary(name: String)
case middle(name: String)
func get() -> String {
switch self {
case .elementary(let name):
return name
case .middle(let name):
return name
}
}
}
let yourMiddleSchoolName = SchoolDetail.middle(name: "Seogeun")
print("Your Middle School Name is \(yourMiddleSchoolName.get())")
// Your Middle School Name is Seogeun
enum School {
case elementary
case middle
case high
// You can also define them on a single line like this:
// case elementary, middle, high
}
let yourSchool = School.elementary
print("YourSchool : \(yourSchool)") // YourSchool : elementary
enum Grade: Int {
case first = 1
case second = 2
}
let yourGrade = Grade.second
print("YourGrade : \(yourGrade.rawValue)") // YourGrade : 2
// Enum with Associated Values
enum SchoolDetail {
case elementary(name: String)
case middle(name: String)
func get() -> String {
switch self {
case .elementary(let name):
return name
case .middle(let name):
return name
}
}
}
let yourMiddleSchoolName = SchoolDetail.middle(name: "Seogeun")
print("Your Middle School Name is \(yourMiddleSchoolName.get())")
// Your Middle School Name is Seogeun
⚖️ Differences Between Classes and Structs
The most significant difference is that Classes are Reference Types, whereas Structs and Enums are Value Types.
Classes support inheritance, but Structs and Enums do not.
The most significant difference is that Classes are Reference Types, whereas Structs and Enums are Value Types.
Classes support inheritance, but Structs and Enums do not.
🤝 Similarities Between Classes and Structs
Both can group different data types together into a single cohesive unit.
Both can define (Instance/Type) Methods and (Instance/Type) Properties.
These grouped structures can be used as entirely custom new types.
Both allow defining functions and properties within their scopes.
Both can be extended using the extension keyword.
Both can group different data types together into a single cohesive unit.
Both can define (Instance/Type) Methods and (Instance/Type) Properties.
These grouped structures can be used as entirely custom new types.
Both allow defining functions and properties within their scopes.
Both can be extended using the extension keyword.
댓글
댓글 쓰기