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 λ₯Ό μμ±νλ€.letkeyword λ₯Ό μ΄μ©ν΄ 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λvarkeyword λ₯Ό μ΄μ©ν΄ λ³μλ‘ μ μΈλμκΈ° λλ¬Έμ΄λ€.
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.