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

  1. 기본적으둜 Structure λ₯Ό 써라
  2. Objective-C와 μƒν˜Έ 운용이 ν•„μš”ν•˜λ©΄ Class λ₯Ό 써라
  3. μ•± μ „μ²΄μ—μ„œ λ°μ΄ν„°μ˜ identity λ₯Ό μ œμ–΄ν•΄μ•Όν•œλ‹€λ©΄ Class λ₯Ό 써라
  4. 곡유 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κ°€ μ΄λ€„μ§€λŠ” κ²ƒμ²˜λŸΌ 보인닀.

Standard Library - Array

반면, 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’이닀.

sharedStateStruct

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

μœ„ 과정을 μ„€λͺ…ν•˜λ©΄ λ‹€μŒκ³Ό 같은 κ°™λ‹€.

  1. VideoMode() instance λ₯Ό μƒμ„±ν•œλ‹€.
  2. let keyword λ₯Ό μ΄μš©ν•΄ tenEighty 에 μœ„μ—μ„œ μƒμ„±ν•œ instance λ₯Ό ν• λ‹Ήν•œλ‹€. 이제 tenEighty λŠ” μœ„μ—μ„œ μƒμ„±ν•œ VideoMode Class의 instance λ₯Ό μ°Έμ‘°ν•œλ‹€.
  3. 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이기 λ•Œλ¬Έμ΄λ‹€.

sharedStateClass


λ”°λΌμ„œ, 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

  1. β€œStructures and Classes.” The Swift Programming Language Swift 5.7. accessed Nov. 21, 2022, Swift Docs Chapter 8 - Structures and Classes.
  2. β€œChoosing Between Structures and Classes.” Apple Developer Documentation. accessed Nov. 21, 2022, Choosing Between Structures and Classes.