Swift Structures and Classes
Choosing Between Structures and Classes
1. Comparing Structures and Classes π©βπ»
μΌλ°μ μΌλ‘ νλ‘κ·Έλλ° μΈμ΄μμ Class νλμ νμΌ νλ
κ° νμνλ€.
νμ§λ§ Swift λ νμΌ νλμ μ¬λ¬ κ°μ Classes μ Structures λ₯Ό μ μ
ν μ μμΌλ©°, μΈλΆ μΈν°νμ΄μ€λ λ€λ₯Έ Class λ
Structure κ° μ¬μ©ν μ μλλ‘ μλμΌλ‘ μ 곡λλ€.
μ΄λ μ ν΅μ μΌλ‘ νλ‘κ·Έλλ° μΈμ΄μμ Class
μ instance
λ Object
μΈ λ°λ©΄, Swift μ Structures
μ
Classes
λ λ€λ₯Έ μΈμ΄μ λΉκ΅ν΄ Functionality
μ κ°κΉλ€.
1. Commons Structures and Classes
- Define properties : κ°μ μ μ₯
- Define methods : κΈ°λ₯μ μ 곡
- Define subscripts :
Subscript Syntax
λ₯Ό μ΄μ©ν΄ κ°μ μ κ·Ό - Define initializers : μ΄κΈ° μνλ₯Ό μ€μ
- Be extended : κΈ°λ³Έ ꡬν μ΄μμΌλ‘ νμ₯
- Conform to protocols : νΉμ μ’ λ₯μ νμ€ κΈ°λ₯μ μ 곡
2. Class Only Features
- inheritance : λ€λ₯Έ Classμ νΉμ±μ inherit (Structure μ Protocol μ λ€λ₯Έ Protocol μ adopt νλ κ²λ§ κ°λ₯νλ€.)
- Runtime λ class instance μ νμ μ ν΄μ(interpret)νκ³ , type casting μ΄ κ°λ₯νλ€.
- deinitializers : class instance μ ν λΉλ μμμ ν΄μ
- Reference counting : class instance μ μ°Έμ‘°λ₯Ό νμ© (Structure λ Value Types λ‘ νμ Copy λλ―λ‘, Reference counting μ μ¬μ©νμ§ μλλ€.)
Class
κ° μ 곡νλ μΆκ° κΈ°λ₯μ 볡μ‘μ±μ μ¦κ°μν¨λ€. λ°λΌμ general guideline μ λ°λ₯΄λ©΄, Class λ₯Ό μ¬μ©νλ κ²μ΄ κΌ νμνκ±°λ λ μ ν©ν κ²½μ°κ° μλλ©΄ μΌλ°μ μΌλ‘ μΆλ‘ νκΈ° μ¬μ΄Structure
λ₯Ό μ νΈν΄μΌνλ€κ³ λ§νλ€. μ΄λ μ°λ¦¬κ° λ§λλ λλΆλΆμ Custom Data Types λ Structure λλ Enumeration μΌλ‘ λμ΄μΌ ν¨μ μλ―Ένλ€.
3. Choosing Between Structures and Classes
μ΄μ λν΄ μ νμ μλ κΈμ ν΅ν΄ λ€μκ³Ό κ°μ΄ μ΄μΌκΈ°νλ€.
Choosing Between Structures and Classes
- κΈ°λ³Έμ μΌλ‘ Structure λ₯Ό μ¨λΌ
- Objective-Cμ μνΈ μ΄μ©μ΄ νμνλ©΄ Class λ₯Ό μ¨λΌ
- μ± μ 체μμ λ°μ΄ν°μ identity λ₯Ό μ μ΄ν΄μΌνλ€λ©΄ Class λ₯Ό μ¨λΌ
- 곡μ implementation(ꡬν체) λ₯Ό μ μ©νκΈ° μν΄ Structure μ Protocol μ μ¨λΌ
μ 3, 4λ²μ λν΄ μ’ λ μμΈν μ€λͺ
ν΄λ³΄μ.
3 ) μ± μ 체μμ λ°μ΄ν°μ identity λ₯Ό μ μ΄ν΄μΌνλ€λ©΄ Class λ₯Ό μ¨λΌ
λ κ°μ μλ‘ λ€λ₯Έ class instance λ μλ‘ κ°μ properties λ₯Ό κ°κ³ , μ μ₯λ κ° μμ λμΌνλλΌλ
identity operator(==)
(λμΌ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό κ°μ‘λκ°?)μ μν΄ μλ‘ λ€λ₯Έ κ²μ νμΈν μ μλ€.
μ¦, class instance λ₯Ό μ± λ΄μμ 곡μ νλ©΄, instance μ λ³κ²½ μ¬νμ΄ ν΄λΉ instance λ₯Ό μ°Έμ‘°νλ λͺ¨λ κ³³μ
곡μ λ¨
μ μλ―Ένλ€. μ΄λ° identity
κ° νμν μλ λ€μκ³Ό κ°λ€.
- file handles
- network connections
- CBCenterManagerμ κ°μ shared hardware intermediaries
μλ₯Ό λ€μ΄ λ‘컬 λ°μ΄ν°λ² μ΄μ€ μ°κ²°μ λ€λ£¨λ μ½λλ μ± μ μμμ λ°μ΄ν°λ² μ΄μ€λ₯Ό μμ ν μ μ΄ν΄μΌνλ€. λ°λΌμ μ΄ κ²½μ° Class λ₯Ό μ¬μ©νλ κ²μ΄ μ ν©νλ€. λ¨, μ±μμ shared database object μ μ κ·Όν μ μλ μμμ μ νν΄μΌνλ€.
Class λ₯Ό μ¬μ©νλ κ²μ μ± λ΄μμ identity λ₯Ό κ΄λ²μνκ² κ³΅μ νλ€λ κ²μ μλ―Ένλ©°, μ΄λ λ Όλ¦¬ μ€λ₯κ° λ°μν κ°λ₯μ±μ λμΈλ€. λ°λΌμ identity λ₯Ό μ μ΄ν νμκ° μλ€λ©΄ Structure λ₯Ό μ¬μ©νλ κ²μ΄ μ ν©νλ€.
4 ) 곡μ implementation(ꡬν체) λ₯Ό μ μ©νκΈ° μν΄ Structure μ Protocol μ μ¨λΌ
Class λ
Class Inheritance
μAdopt Protocol
μ μ§μνλ€. Structure μ Protocol μAdopt Protocol
λ§ μ§μνλ€.
κ·Έλ¬λ Structureμ Protocol λ§μΌλ‘λ μΆ©λΆν Class Inheritance μ κ°μ inheritance hierarchies(μμ κ³μΈ΅)μ ꡬνμ΄ κ°λ₯νλ€.
inheritance
κ΄κ³λ₯Ό μ€κ³ν λ μ²μλΆν° Protocol
μ μ¬μ©νλ κ²μ΄ μ’λ€. Class λ₯Ό μμν μ μλ κ²μ
Class λ‘ μ νλμ§λ§, Protocol
μ Class λΏ μλλΌ Structure, Protocol κΉμ§ λͺ¨λμκ² μ±νμ
νμ©νκΈ° λλ¬Έμ λͺ¨λΈλ§μ΄ ν¨μ¬ λ μ μ°ν΄μ§λ€.
λ§μ½,
Class μμλ§ μ ν¨ν΄μΌνλ μμ
μ ꡬνν λ, Class Inheritance λμ Class-Only Protocols λ₯Ό μ¬μ©ν μ μλ€.
2. Definition of Structures and Classes π©βπ»
1. Definition Syntax
struct SomeStructure {
// structure definition goes here
}
class SomeClass {
// class definition goes here
}
Structure
λ μμstruct
ν€μλλ₯Ό λΆμ¬ μ μνλ€.Class
λ μμclass
ν€μλλ₯Ό λΆμ¬ μ μνλ€.
μ Structure λ Class λ₯Ό μ μν λλ§λ€ new Swift Type
μ μ μνλ κ²μ΄λ€. Enumeration
μ μ μν λΌ
μ΄κ²μ΄ Swift μ λ€λ₯Έ Typesμ λ§μ°¬κ°μ§λ‘ μ΄λ¦μ λλ¬Έμλ‘ μμνλ€. Structure μμ λμΌνκ² λλ¬Έμλ‘ μμ
νλ€.
λ°λ©΄ Properties
μ Methods
λ Type Names
(Structures, Classes μ μ΄λ¦)κ³Ό ꡬλΆμ μν΄ μλ¬Έμλ‘ μμ
νλ€.
2. Structure and Class Instances
λ€μ μ½λλ Swift μ Resolution
κ³Ό VideoMode
λΌλ μ Types
λ₯Ό λ§λ€μ΄λΈλ€.
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var interlaced = false
var frameRate = 0.0
var name: String?
}
Syntax for Creating instance
let someResolution = Resolution()
let someVideoMode = VideoMode()
Structure μ Class λ λ€ new instance λ₯Ό μμ±νκΈ° μν΄ initializer
λ₯Ό μ¬μ©νλ©° Syntax λ μ μ¬νλ€.
3. Accessing Properties
1 ) Get Properties
instance μ properties μ μ κ·Όνλ κ°μ₯ μ¬μ΄ λ°©λ²μ dot Syntax
λ₯Ό μ¬μ©νλ κ²μ΄λ€.
instance μ΄λ¦ λ€μ .
μ λΆμ΄κ³ , κ·Έ λ€μ property μ΄λ¦μ μ λλ€.
print("The width of someResolution is \(someResolution.width)")
// Prints "The width of someResolution is 0"
dot Syntax λ₯Ό μ΄μ©ν΄ subproperties
μ drill down
ν΄ λ΄λ €κ°λ κ²λ κ°λ₯νλ€.
λ€μμ VideoMode instance μ resolution property μ μ κ·Όν΄ subproperty μΈ width λ‘
drill down ν΄ λ΄λ €κ°λ€.
// drill down into subproperties
print("The width of someVideoMode is \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is 0"
2 ) Set Properties
dot Syntax λ₯Ό μ΄μ©ν΄ properties μ μ κ·Όνλ κ²μ λ¬Όλ‘ , variable μ κ°μ ν λΉν μ μλ€.
someVideoMode.resolution.width = 1280
print("The width of someVideoMode is now \(someVideoMode.resolution.width)")
// Prints "The width of someVideoMode is now 1280"
someResolution.width = 1600 // Cannot assign to property: 'someResolution' is a 'let' constant
νμ§λ§ Structure μ κ²½μ° Properties λ₯Ό var
λ‘ μ μΈνμμλ instance μ체λ₯Ό let
μΌλ‘ μ μΈνλ©΄ κ°μ
ν λΉνλ € ν λ μλ¬κ° λ°μνλ€.
μ΄λ, Class λ
Reference Type
μΌλ‘ instance λ₯Όlet
μΌλ‘ μ μΈνλ€ νλλΌλ λ³κ²½νλ €λ μ€μ Valueμ ν΄λΉνλ Properties λvar
λ‘ μ μΈλμ΄mutable
ν λ°λ©΄, Structure λValue Type
μΌλ‘let
μΌλ‘ μ μΈλ instance κ·Έ μ체κ°immutable
νκΈ° λλ¬Έμ΄λ€.
λ°λΌμ Structure μ κ²½μ° Properties λ₯Ό μμ νλ €λ©΄ instance μμ var
λ‘ μ μΈν΄μΌνλ€.
var anotherResolution = Resolution()
anotherResolution.width = 1600
print("The width of anotherResolution is now \(anotherResolution.width)")
// Prints "The width of anotherResolution is now 1600"
4. Memberwise Initializers for Structure Types
let resolutionTypeA = Resolution(width: 5120, height: 2880)
print(resolutionTypeA) // Resolution(width: 5120, height: 2880)
let resolutionTypeB = Resolution(width: 2560)
print(resolutionTypeB) // Resolution(width: 2560, height: 0)
let resolutionTypeC = Resolution(height: 1440)
print(resolutionTypeC) // Resolution(width: 0, height: 1440)
Structures λ Classes μ λ¬λ¦¬ Mmeberwise Initializers
λ₯Ό κ°μ§ μ μμΌλ©° μλ μμ±λλ 쑰건μ λ€μκ³Ό κ°λ€.
- μ‘΄μ¬νλ
Initializers
κ° νλλ μλ€
Default Initializers λ μ‘΄μ¬νλ Initializers κ° μκ³ , default value
λ₯Ό κ°μ§κ³ μμ΄μΌνλ€.
νμ§λ§ Mmeberwise Initializers λ default value λ κ°μ§κ³ μμ§ μμλ 무κ΄νλ€. λ¨μ§ μ‘΄μ¬νλ
Initializers κ° νλλ μμΌλ©΄ λλ€. default value μ μ‘΄μ¬ μ 무μ λ°λΌ μλμΌλ‘ μμ±λλ
Mmeberwise Initializers μ κ°μλ§ λ¬λΌμ§ λΏμ΄λ€.
3. Structures and Enumerations Are Value Types π©βπ»
1. Characteristics of Value Types
Value Type
μ Variable λλ Constant μassign λ λ
, 그리κ³ν¨μμ μ λ¬λ λ
μ 체κ°copy
λλ€.Swift μ λͺ¨λ κΈ°λ³Έ νμ λ€, integers, floating-point Numbers, Booleans, strings, arrays, dictionaries λ λͺ¨λ
Value Types
μΌλ‘Structures λ‘ κ΅¬ν
λμ΄μλ€.
Standard Library
μ μν΄ μ μλ Array, Dictionary κ·Έλ¦¬κ³ String κ³Ό κ°μCollections
μμ Structures λ‘ κ΅¬νλμ΄ μμΌλ―λ‘Value Types
λ€.νμ§λ§ λ€λ₯Έ Value Types μ λ€λ₯΄κ²
performance cost of copying
μ μ€μ΄κΈ° μν΄optimiaztion
μ μ¬μ©νλ€. λ°λΌμ, Value Types κ° μ¦μ copy λ₯Ό νλ κ²κ³Ό λ€λ₯΄κ² copies μ μμ μ΄ λ°μλκΈ° μ μλReference Types
μ²λΌoriginal instance
μcopies
κ° λ©λͺ¨λ¦¬λ₯Ό 곡μ νλ€.μ΄ν copies μ€ νλμ μμ μ΄ λ°μνλ©΄, μμ μ΄ λκΈ° μ§μ μ μ€μ
copy
κ° μ΄λ£¨μ΄μ§λ€. μ¦,copies
μ μμ μ΄ λ°μλκΈ° μ΄μ μλReference Types
μ²λΌ μλνκ³ , μμ μ΄ λ°μλλ μκ°Value Types
μ²λΌ μλνκΈ° λλ¬Έμ μ½λμμΌλ‘λ μ¦μcopy
κ° μ΄λ€μ§λ κ²μ²λΌ 보μΈλ€.
λ°λ©΄,
Foundation
μ μν΄ μ μλ NSArray, NSDictionary κ·Έλ¦¬κ³ NSString κ³Ό κ°μClasses Bridged to Swift Standard Library Value Types
λ Classes λ‘ κ΅¬νλμ΄ μμΌλ―λ‘Reference Types
λ€.
Foundation - NSArray, Classes Bridged to Swift Standard Library Value Types
2. Structures
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
μμ hd structure λ₯Ό λ§λ€κ³ , μ΄λ₯Ό cinema λ³μμ 볡μ¬νλ€.
cinema.width = 2048
μ΄μ cinema μ width λ β2048βμ΄λ€.
print("cinema is now \(cinema.width) pixel wide")
// Prints "cinema is now 2048 pixels wide"
print("hd is still \(hd.width) pixel wide")
// Prints "hd is still 1920 pixels wide"
cinema μ width λ β2048βλ‘ λ³κ²½λμμΌλ, hd μ width λ μ¬μ ν β1920βμ΄λ€.
3. Enumerations
enum CompassPoint {
case north, south, east, west
mutating func turnNorth() {
self = .north
}
}
var currentDirection = CompassPoint.west
let rememberedDirection = currentDirection
λ³μ currentDirection
enumeration μ λ§λ€κ³ , μ΄λ₯Ό rememberedDirection
λ³μμ 볡μ¬νλ€.
//currentDirection = .north
currentDirection.turnNorth()
μ΄μ currentDirection
λ βnorthβ λ€.
print("The current direction is \(currentDirection)")
// Prints "The current direction is north"
print("The remembered direction is \(rememberedDirection)")
// Prints "The remembered direction is west"
currentDirection
λ βnorthβ λ‘ λ³κ²½λμμΌλ, rememberedDirection
λ μ¬μ ν βwestβ λ€.
4. Classes Are Reference Types π©βπ»
1. Characteristics of Reference Types
Reference Type
μ Variable λλ Constant μ ν λΉλ λ, κ·Έλ¦¬κ³ ν¨μμ μ λ¬λ λ copy λμ§ μλλ€.
copy λμ λμΌinstance
λ₯Όμ°Έμ‘°(reference)
νλ€.
2. Classes
let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.interlaced = true
tenEighty.name = "1080i"
tenEighty.frameRate = 25.0
μ κ³Όμ μ μ€λͺ νλ©΄ λ€μκ³Ό κ°μ κ°λ€.
VideoMode()
instance λ₯Ό μμ±νλ€.let
keyword λ₯Ό μ΄μ©ν΄ tenEighty μ μμμ μμ±ν instance λ₯Ό ν λΉνλ€. μ΄μ tenEighty λ μμμ μμ±νVideoMode Class
μ instance λ₯Ό μ°Έμ‘°νλ€.- tenEighty μ members μ κ°κ° κ°μ ν λΉνλ€.
let alsoTenEighty = tenEighty
μλ‘μ΄ μμ alsoTenEighty μ alsoTenEighty λ₯Ό ν λΉνλ€.
alsoTenEighty.frameRate = 30.0
alsoTenEighty μ frameRate λ₯Ό β30.0βμΌλ‘ μμ νλ€.
print("The frameRate property of alsoTenEighty is now \(alsoTenEighty.frameRate)")
print("The frameRate property of tenEighty is now \(tenEighty.frameRate)")
The frameRate property of alsoTenEighty is now 30.0
The frameRate property of tenEighty is now 30.0
alsoTenEighty λ₯Ό μμ νλλ° tenEighty λ κ°μ΄ λ°λ κ°μ μΆλ ₯νλ€!
Classes
λ Reference Types
μ΄κΈ° λλ¬Έμ΄λ€.
λ°λΌμ, tenEighty, alsoTenEighty λ₯Ό μ§μ μΆλ ₯ν΄λ³΄λ©΄ class μ members μ체μ κ°
μ μΆλ ₯νλ κ²μ΄
μλλΌ κ·Έ member κ° μ°Έμ‘°νλ λμμ μΆλ ₯νλ€.
tenEighty, alsoTenEighty λ₯Ό
let
μΌλ‘ μ μΈνλλ°λ μμ μ΄ κ°λ₯ν μ΄μ μμ, tenEighty, alsoTenEighty κ°constnat
μΈ κ²μΌ λΏ, μ΄κ²μ΄ μ°Έμ‘°νλ instance κ° κ°κ³ μλmemebr frameRate
λvar
keyword λ₯Ό μ΄μ©ν΄ λ³μλ‘ μ μΈλμκΈ° λλ¬Έμ΄λ€.
print(tenEighty) // __lldb_expr_11.VideoMode
print(alsoTenEighty) // __lldb_expr_11.VideoMode
3. Identity Operators
κΈ°μ‘΄μ μκ³ μλ equal to
, not equal to
Operators λ λ€μκ³Ό κ°λ€.
// Equal to(==)
print(5 == 5) // true
print(5 == 7) // false
// Not equal to(!=)
print(5 != 7) // true
Reference Types
λ₯Ό μν Identity Operators
λ ==
, !=
κ° μλ ===
, !==
λ₯Ό μ¬μ©νλ€.
print(tenEighty === alsoTenEighty) // true
print(tenEighty !== alsoTenEighty) // false
if tenEighty === alsoTenEighty {
print("tenEighty and alsoTenEighty refer to the same VideoMode instance.")
}
// Prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."
tenEighty μ *alsoTenEighty *λ VideoMode Class μ λμΌν instance λ₯Ό μ°Έμ‘°
νκ³ μμμ μ μ μλ€.
let numA = 5
let numB = numA
print(numA === numB) // error: argument type 'Int' is not a reference types
===
λλ !==
operators λ Reference Types
λ₯Ό μν Identity Operators
λ‘, Value Types λ₯Ό
λΉκ΅νλ €κ³ νλ©΄ μλ¬κ° λ°μνλ€.
4. Pointers
C, C++, Objective-C κ°μ μΈμ΄λ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό μ°Έμ‘°νκΈ° μν΄ pointer
λ₯Ό μ¬μ©νλ€.
μ΄κ²μ Swift μμ Reference Types
μ instance
λ₯Ό μ°Έμ‘°νκΈ° μν μμ λλ λ³μ μμ μ΄μ μ μ¬νλ€.
νμ§λ§, Swift κ° κ°λ¦¬ν€λ μ£Όμκ°μ C μΈμ΄μμμ pointer μ λ¬λ¦¬ λ©λͺ¨λ¦¬ μ£Όμλ₯Ό κ°λ¦¬ν€λdirect pointer
κ°
μλλ©°, reference λ₯Ό λ§λ€κΈ° μν΄ asterisk(*)
λ₯Ό νμλ‘ νμ§ μλλ€.
Swift μμ referencesλ λ€λ₯Έ constants λλ variablesλ₯Ό μ μνλ― μ¬μ©νλ©΄ λλ€.
λ§μ½, pointer
λ₯Ό μ§μ μ μΌλ‘ λ€λ€μΌ νλ κ²½μ°λ₯Ό μν΄ Standard Library
λ pointer types
μ buffer types
λ₯Ό
μ 곡νλ€. Manual Memory Management
Reference
- βStructures and Classes.β The Swift Programming Language Swift 5.7. accessed Nov. 21, 2022, Swift Docs Chapter 8 - Structures and Classes.
- βChoosing Between Structures and Classes.β Apple Developer Documentation. accessed Nov. 21, 2022, Choosing Between Structures and Classes.