1. Access Control ๐Ÿ‘ฉโ€๐Ÿ’ป

Access Control ์€ ๋‹ค๋ฅธ ์†Œ์Šค ํŒŒ์ผ๊ณผ ๋ชจ๋“ˆ์—์„œ ์ฝ”๋“œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ์ œํ•œํ•œ๋‹ค. Access Control ์„ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ์€๋‹‰ํ™”(hiding) ํ•˜๊ณ  ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ณธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ‘๊ทผ ์ˆ˜์ค€์€ Classes, Structures, Enumerations ๋‹จ์œ„๋กœ ์ œํ•œํ•˜๊ฑฐ๋‚˜ ์ด์— ์†ํ•ด ์žˆ๋Š” Properties, Methods, Initializers, Subscripts ๋‹จ์œ„๋กœ ์ œํ•œํ•˜๋Š” ๊ฒƒ ์—ญ์‹œ ๊ฐ€๋Šฅํ•˜๋‹ค. ๋˜ํ•œ Protocols ๋Š” ํŠน์ • context ๋‹จ์œ„๋กœ ์ œํ•œ๋  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋ฌธ์„œ์—์„œ๋Š” ๊ฐ„๊ฒฐ์„ฑ์„ ์œ„ํ•ด Properties, Types, Functions ๋“ฑ๊ณผ ๊ฐ™์ด Access Control ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์„ Entities๋กœ ํ‘œํ˜„ํ•œ๋‹ค.


2. Modules and Source Files ๐Ÿ‘ฉโ€๐Ÿ’ป

Swift ์˜ Access Control ์€ Modules์™€ Source Files์˜ ๊ฐœ๋…์„ ๊ธฐ์ดˆ๋กœ ํ•œ๋‹ค.

  • Modules : Single unit of code distribution. ์ฆ‰, ์ฝ”๋“œ๊ฐ€ ๋นŒ๋“œ๋˜๊ณ  ์ œ๊ณต๋˜๋Š” Framework ๋˜๋Š” App ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ์˜ ๋ฐฐํฌ ๋‹จ์œ„๋กœ import keyword ๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•œ๋‹ค.
  • Source Files : Modules ๋‚ด์˜ Single Swift Source Code File ๋กœ ์‹ค์ œ๋กœ Framework ๋˜๋Š” App ๋‚ด์— ์žˆ๋Š” Single File ์„ ์˜๋ฏธํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฅธ Types ๋Š” ๊ฐ๊ฐ์˜ Source Files ์— ์ •์˜ํ•˜์ง€๋งŒ Swift ์—์„œ๋Š” Single Source Code File ์— ์—ฌ๋Ÿฌ Types, Functions ๋“ฑ์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

3. Access Levels ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Access Levels

Swift ๋Š” ์ฝ”๋“œ ๋‚ด์—์„œ Entities ์— ๋Œ€ํ•ด 5๊ฐœ์˜ ๋‹ค๋ฅธ Access Levels ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด Access Levels ๋Š” Modules > Source Files > Entities์˜ Hierarchy ๊ตฌ์กฐ์™€ ๊ด€๋ จ๋œ๋‹ค.

  • Open : Framework Level, App ๋˜๋Š” Framework ๋ฅผ ๊ณต๊ฐœ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
    (Public ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ Open ์€ Classes ์™€ Class Members ์—๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ Open ์œผ๋กœ ์ •์˜๋œ Classes ์™€ Class Members ๋ฅผ Subclassing, Overriding ํ•˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•œ๋‹ค.)
  • Public : Framework Level, App ๋˜๋Š” Framework ๋ฅผ ๊ณต๊ฐœ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • Internal : Application Level, ์ƒ๋žต์‹œ ์ ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ์ ‘๊ทผ ๋ ˆ๋ฒจ๋กœ, ๋™์ผ ๋ชจ๋“ˆ ๋‚ด์—์„œ ์ž์œ ๋กœ์šด ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ์™ธ๋ถ€ ๋ชจ๋“ˆ์˜ ์ ‘๊ทผ์€ ์ œํ•œ๋œ๋‹ค. ๋”ฐ๋ผ์„œ App ๋˜๋Š” Framework ์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • File-private : Application Level, Source File ๋‚ด๋ถ€๋กœ ์ ‘๊ทผ์„ ์ œํ•œํ•œ๋‹ค.
  • Private : Application Level, Source File ์•ˆ์—์„œ๋„ ์ •์˜๋ฅผ ๋‘˜๋Ÿฌ์‹ผ context ๋กœ ๋‚ด๋ถ€๋กœ ์ ‘๊ทผ์„ ์ œํ•œํ•œ๋‹ค.

2. Guiding Principle of Access Levels

  • Public Variables๋Š” Internal, File-private, Private Types ๋กœ ์ •์˜๋  ์ˆ˜ ์—†๋‹ค. Public Variables ๋Š” ์–ด๋””์„œ๋‚˜ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์ง€๋งŒ Access Levels ๋ฅผ ์ด๋ณด๋‹ค ๋‚ฎ์€ ์ˆ˜์ค€์œผ๋กœ ๋ณ€๊ฒฝํ•  ๊ฒฝ์šฐ ๊ทธ๋ ‡์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • Functions๋Š” Parameter ์™€ Return Types ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.

3. Default Access Levels

๋ช…์‹œ์ ์œผ๋กœ Access Levels ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด Swift ๋Š” default ๋กœ Internal์„ Access Levels ๋กœ ๊ฐ–๋„๋ก ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ Access Levels ๋ฅผ ๋ช…์‹œํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

4. Access Levels for Single-Target Apps

Single-Target App์„ ๋งŒ๋“ค ๋•Œ ์•ฑ์˜ ์ฝ”๋“œ๋Š” ์•ฑ ๋‚ด์— ํฌํ•จ๋˜๋ฉฐ ์•ฑ์˜ ์™ธ๋ถ€ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•˜๋„๋ก ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค. Swift ์— ์˜ํ•ด default ๋กœ ์ง€์ •๋˜๋Š” Internal์€ ์ด์— ์ ํ•ฉํ•˜๋ฏ€๋กœ Access Levels ๋ฅผ ๋ช…์‹œํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋‹จ, ์•ฑ์˜ ๋ชจ๋“ˆ ๋‚ด๋ถ€ ๋‹ค๋ฅธ ์ฝ”๋“œ์—๊ฒŒ ๊ตฌํ˜„์˜ ์„ธ๋ถ€ ๋‚ด์šฉ์„ ๊ฐ€๋ฆฌ๊ธฐ ์œ„ํ•ด File Private, Private์„ ์ด์šฉํ•ด Access Levels ๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

5. Access Levels for Frameworks

Framework ๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก Framework ์˜ ๊ณต์šฉ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ open ๋˜๋Š” public์œผ๋กœ ํ‘œ์‹œํ•œ๋‹ค.

Framework ๋‚ด์˜ ํ—ค๋ถ€ ๊ตฌํ˜„์€ Default Access Levels ์ธ internal์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, Framework ๋‚ด๋ถ€์—์„œ๋„ ๋‹ค๋ฅธ ์ฝ”๋“œ ๋ถ€๋ถ„์— ๊ฐ€๋ฆฌ๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” fileprivate ๋˜๋Š” private์„ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. Framework ๊ฐ€ ๋…ธ์ถœ ์‹œํ‚ฌ API ์— ํ•ด๋‹นํ•˜๋Š” Entities ์—๋งŒ open, public์„ ๋ช…์‹œํ•˜๋ฉด ๋œ๋‹ค.

6. Access Levels for Unit Test Targets

Unit Test Targets ์„ ์ด์šฉํ•ด ์•ฑ์„ ๊ตฌํ˜„ํ•  ๋•Œ ํ•ด๋‹น ์ฝ”๋“œ๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด Test ๋ชจ๋“ˆ ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ open ๋˜๋Š” public์œผ๋กœ ์„ ์–ธ๋˜์ง€ ์•Š์€ Entities ๋Š” ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

๋‹จ, Unit Test Targets ์— ํ•œํ•ด ๋ชจ๋“ˆ์„ import ํ•  ๋•Œ ์•ž์— @testable attribute ๋ช…์‹œํ•˜๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ์˜ internal entities ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


4. Access Control Syntax ๐Ÿ‘ฉโ€๐Ÿ’ป

์œ„์—์„œ ์„ค๋ช…ํ•œ Access Levels ์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด open, public, internal, fileprivate, private modifier ๋ฅผ Entities ์˜ ์ •์˜ ์•ž์— ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•œ Syntax ๋‹ค.

public class SomePublicClass {}
internal class SomeInternalClass {}
fileprivate class SomeFilePrivateClass {}
private class SomePrivateClass {}

public var somePublicVariable = 0
internal let someInternalConstant = 0
fileprivate func someFilePrivateFunction() {}
private func somePrivateFunction() {}

๊ทธ๋ฆฌ๊ณ  ์œ„์—์„œ internal ๋กœ ์„ ์–ธ๋œ SomeInternalClass์™€ someInternalConstant๋Š” ๋ณ„๋„๋กœ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  default Access Levels ๊ฐ€ ์ ์šฉ๋˜๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

class SomeInternalClass {}              // implicitly internal
let someInternalConstant = 0            // implicitly internal

5. Custom Types ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Custom Types

User Custom Types ๋ฅผ ์ •์˜ํ•  ๋•Œ Access Levels ์ •์˜ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด Types ๋ฅผ ์ •์˜ํ•  ๋•Œ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค. Types ์ž์ฒด์— ๋Œ€ํ•œ Access Levels ๋Š” ํ•ด๋‹น Types ๊ฐ€ ๊ฐ–๋Š” Members(Properties, Methods, Initializers, Subscripts) ์˜ default Access Levels ์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

  • Types ๋ฅผ fileprivate์œผ๋กœ ์ •์˜ํ•˜๋ฉด, ๊ทธ Members ์—ญ์‹œ fileprivate์ด ๋œ๋‹ค.
  • ๋‹จ, Types ๋ฅผ public์œผ๋กœ ์ •์˜ํ•˜๋”๋ผ๋„ ๊ทธ Members ๋Š” internal์ด๋‹ค. ์ด๋Š” ์‹ค์ˆ˜๋กœ ๋ชจ๋“ˆ์˜ API ๊ฐ€ ์•„๋‹Œ ์ฝ”๋“œ ๋ถ€๋ถ„์ด ๋…ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

Access Levels

  • (open, public) Types = internal Members
  • (internal, fileprivate, private) Types = Members
public class SomePublicClass {                      // explicitly public class
    public var somePublicProperty = 0               // explicitly public class member
    var someInternalProperty = 0                    // implicitly internal class member
    fileprivate func someFilePrivateMethod() {}     // explicitly file-private class member
    private func somePrivateMethod() {}             // explicitly private class member
}

class SomeInternalClass {                           // implicitly internal class
    var someInternalProperty = 0                    // implicitly internal class member
    fileprivate func someFilePrivateMethod() {}     // explicitly file-private class member
    private func somePrivateMethod() {}             // explicitly private class member
}

fileprivate class SomeFilePrivateClass {            // explicitly file-private class
    func someFilePrivateMethod() {}                 // implicitly file-private class member
    private func somePrivateMethod() {}             // explicitly private class member
}

private class SomePrivateClass {                    // explicitly private class
    func somePrivateMethod() {}                     // implicitly private class member
}

2. Tuple Types

  • Tuples ๋Š” Classes, Structures, Enumerations, Functions ์™€ ๋‹ฌ๋ฆฌ ๋…๋ฆฝ์ ์ธ ์ •์˜๊ฐ€ ์—†๋‹ค
  • Tuples ์˜ Access Levels ๋Š” ๊ตฌ์„ฑ๋œ Types ๋กœ๋ถ€ํ„ฐ ์ž๋™์œผ๋กœ ์ •ํ•ด์ง€๋ฉฐ, ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.
  • Tuples ์˜ Access Levels ๋Š” ๊ตฌ์„ฑ๋œ Types ์ค‘ ๊ฐ€์žฅ ๋‚ฎ์€ ์ˆ˜์ค€์˜ Access Levels ๋กœ ์ž๋™์œผ๋กœ ์ •ํ•ด์ง„๋‹ค.

Access Levels

  • Tuples โ‰ค min(Types1, Types2)

๋”ฐ๋ผ์„œ internal๊ณผ private์œผ๋กœ ๊ตฌ์„ฑ๋œ Tuples ์˜ Access Levels ๋Š” private์ด ๋œ๋‹ค.

3. Function Types

  • Functions ์˜ Access Levels ๋Š” Parameter Types ์™€ Return Types ์ค‘ ๊ฐ€์žฅ ๋‚ฎ์€ ์ˆ˜์ค€์˜ Access Levels ๋กœ ๊ณ„์‚ฐ๋˜๋ฉฐ, context ์˜ Access Levels ์™€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

Access Levels

  • Functions โ‰ค min(Parameters, Returns)


1 ) Context ์˜ Access Levels ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ

struct SomeInternalStructure {
    func someFunction() -> (SomeInternalClass, SomeInternalClass) {
        (SomeInternalClass(), SomeInternalClass())
    }
}

context ์˜ Access Levels ๊ฐ€ internal, Function Parameter Types ์™€ Return Types ์˜ Access Levels ๊ฐ€ internal ์ด๋ฏ€๋กœ ํ•จ์ˆ˜๋Š” ์•”์‹œ์ ์œผ๋กœ internal๋กœ ์ •์˜๋œ๋‹ค.

private struct SomePrivateStructure {
    func someFunction() -> (SomePrivateClass, SomePrivateClass) {
        (SomePrivateClass(), SomePrivateClass())
    }
}

context ์˜ Access Levels ๊ฐ€ private, Function Parameter Types ์™€ Return Types ์˜ Access Levels ๊ฐ€ private ์ด๋ฏ€๋กœ ํ•จ์ˆ˜๋Š” ์•”์‹œ์ ์œผ๋กœ private์œผ๋กœ ์ •์˜๋œ๋‹ค.


2 ) Context ์˜ Access levels ์™€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

struct SomeInternalStructure {
    func someFunction() -> (SomeInternalClass, SomePrivateClass) {
        (SomeInternalClass(), SomePrivateClass())
    }  // error: method must be declared fileprivate because its result uses a private type
}

context ์˜ Access Levels ๋Š” internal์ธ๋ฐ, Function Parameter Types ์™€ Return Types ์ค‘ ๋‚ฎ์€ Access Levels ๊ฐ€ private์ด๋ฏ€๋กœ Access Levels ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…์‹œ์ ์œผ๋กœ fileprivate ๋˜๋Š” private ์œผ๋กœ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

struct SomeInternalStructure {
    fileprivate func someFunctionFirst() -> (SomeInternalClass, SomePrivateClass) {
        (SomeInternalClass(), SomePrivateClass())
    }
    private func someFunctionSecond() -> (SomeInternalClass, SomePrivateClass) {
        (SomeInternalClass(), SomePrivateClass())
    }
}

let some = SomeInternalStructure()
some.someFunctionFirst()
some.someFunctionSecond()   // 'someFunctionSecond' is inaccessible due to 'private' protection level

๊ฐ€์žฅ ๋‚ฎ์€ Access Levels ๋Š” private ์ด์ง€๋งŒ fileprivate ๊นŒ์ง€๋Š” ํ—ˆ์šฉ์ด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ๋ฌผ๋ก , ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ Function Parameter Types ์™€ Return Types ์— ๋Œ€ํ•ด private ๋ณด๋‹ค ๋†’์€ fileprivate ์ด ํ—ˆ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ fileprivate ์œผ๋กœ ์„ ์–ธํ•˜๋ฉด ๊ฐ™์€ ํŒŒ์ผ์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ•ด๋‹น Types ์™ธ๋ถ€์—์„œ ๋ณผ ๋•Œ๋Š” private ๊ณผ ๋‹ค๋ฅธ Access Levels ๋ฅผ ๊ฐ–๊ฒŒ ๋œ๋‹ค.

4. Enumeration Types

  • Enumerations ์˜ Cases ์—ญ์‹œ Enumerations ์˜ Access Levels ๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
  • Enumerations ์˜ Cases ๋Š” Classes ๋‚˜ Structures ์˜ Members ์™€ ๋‹ฌ๋ฆฌ Access Levels ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์—†๋‹ค.
  • Enumerations ์— ์‚ฌ์šฉ๋œ Associated Values, Raw Values ๋Š” Enumerations ์˜ Access Levels ๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค.

Access Levels

  • Cases ์˜ Access Levels ์ˆ˜์ • ๋ถˆ๊ฐ€
  • Enumerations = Cases
  • Enumerations โ‰ค Associated Values
  • Enumerations โ‰ค Raw Values

5. Nested Types

  • Nested Types ์—ญ์‹œ context ์˜ Access Levels ๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
  • ๋‹จ, Public Types ์˜ Nested Types ๋Š” internal์ด๋‹ค. (cf. Custom Types)

Access Levels

  • (open, public) Context Types = internal Nested Types
  • (internal, fileprivate, private) Context Types = Nested Types

6. Subclassing ๐Ÿ‘ฉโ€๐Ÿ’ป

  • ๋™์ผ ๋ชจ๋“ˆ์ผ ๊ฒฝ์šฐ ํ˜„์žฌ context ๊ฐ€ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์–ด๋–ค Class ๋“  Subclassing ํ•ด Members ๋ฅผ Overriding ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ๊ฒฝ์šฐ ๋Œ€์ƒ์ด Open Class ๋ผ๋ฉด Subclassing ํ•ด Members ๋ฅผ Overriding ํ•  ์ˆ˜ ์žˆ๋‹ค..
  • Subclass ๋Š” ์ƒ์œ„ Class ๋ณด๋‹ค ๋†’์€ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.

Access Levels

  • Subclass โ‰ค Superclass
  • Overriding ์„ ์ด์šฉํ•ด Subclass Members ์˜ Access Levels ๋ฅผ Superclass ๋ณด๋‹ค ๋†’๊ฒŒ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


Overriding ์„ ์ด์šฉํ•ด ํ•ด๋‹น Class Member ๋ฅผ ๋ณด๋‹ค ์•ก์„ธ์Šค ํ•˜๊ธฐ ์‰ฝ๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

public class A {
    fileprivate func someMethod() {}
}

internal class B: A {
    override internal func someMethod() {}
}
public class A {
    fileprivate func someMethod() {}
}

internal class B: A {
    override internal func someMethod() {
        super.someMethod()
    }
}
  • Subclassing ๋œ Subclass B๋Š” Superclass A ๋ณด๋‹ค ๋†’์ง€ ์•Š์€ Access Levels ๋ฅผ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•œ๋‹ค.
  • Subclassing ์„ ํ†ตํ•ด Subclass B ๋Š” fileprivate์œผ๋กœ ์ •์˜๋œ someMethod()์˜ Access Levels ๋ฅผ Overriding ์„ ํ†ตํ•ด internal๋กœ ๋†’์—ฌ ๋ณด๋‹ค ์—‘์„ธ์Šค ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค.

7. Members ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Constants, Variables, Properties, and Subscripts

  • ์„ ์–ธ๋˜๋Š” Constants, Variables, Properties ๋Š” ํ• ๋‹นํ•˜๋ ค๋Š” Types ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.
  • ์œ ์‚ฌํ•˜๊ฒŒ Subscripts ๋Š” Index Types ๋˜๋Š” Return Types ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.

Access Levels

  • Constants, Variables, Properties โ‰ค Types to assignment
  • Subscripts โ‰ค min(Index, Return)


var internalInstance = SomePrivateClass()   // Variable must be declared private or fileprivate because its type 'SomePrivateClass' uses a private type
fileprivate var fileprivateInstance = SomePrivateClass()
private var privateInstance = SomePrivateClass()

Private Types๋ฅผ ํ• ๋‹นํ•˜๋ฏ€๋กœ ์„ ์–ธ๋˜๋Š” Variables ๋Š” private ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ private์œผ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•œ๋‹ค.

Function Types ์—์„œ ๋ณธ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ private ์ด ์˜ˆ์ƒ๋˜๋Š” ๊ณณ์— fileprivate ๊นŒ์ง€๋Š” ํ—ˆ์šฉ์ด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

2. Getters and Setters

  • Constants, Variables, Properties, Subscripts ์— ๋Œ€ํ•œ Getters์™€ Setters๋Š” ์†ํ•ด ์žˆ๋Š” ๋Œ€์ƒ์˜ Access Levels ๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
  • Setters์˜ Access Levels ๋ฅผ Getters์˜ Access Levels ๋ณด๋‹ค ๋‚ฎ๊ฒŒ ์ œํ•œํ•˜๊ธฐ ์œ„ํ•ด fileprivate(set), private(set) ๋˜๋Š” inernal(set)์„ ์ž‘์„ฑํ•ด ๋” ๋‚ฎ์€ ์ ‘๊ทผ ์ˆ˜์ค€์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

Stored Properties ์— ๋Œ€ํ•ด ๋ช…์‹œ์ ์œผ๋กœ Getters ์™€ Setters ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ Swift ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ Stored Properties ์˜ Backing Storage์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์•”์‹œ์ ์œผ๋กœ Getters ์™€ Setters ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

Access Levels

  • Getters, Setters of (Constants, Variables, Properties, Subscripts) โ‰ค Constants, Variables, Properties, Subscripts
  • Getters = Constants, Variables, Properties, Subscripts
  • Setters โ‰ค Setters

Function Types, Constants, Variables, Properties, and Subscripts ์—์„œ ์•”์‹œ์ ์œผ๋กœ private ์ด ์š”๊ตฌ๋˜๋Š” ๊ณณ์— fileprivate ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ—ˆ์šฉ๋˜์—ˆ์œผ๋‚˜ Setter ์˜ ๊ฒฝ์šฐ ์ข€ ๋” ์—„๊ฒฉํ•˜๊ฒŒ ์ด๋ฅผ ์ง€ํ‚จ๋‹ค. ์ฆ‰, private ์— fileprivate โ€˜Settersโ€™ ๋Š” ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.


fileprivate(set), private(set), inernal(set) ์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์–ด๋–ค์‹์œผ๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.


1 ) Getters: internal, Setters: internal

class SomeClass {
    var id: String = ""
}

์œ„์™€ ๊ฐ™์ด ์ •์˜๋œ SomeClass ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

class SomeClass {
    private var _id: String = ""
    var id: String {
        get {
            _id
        }
        set {
            _id = newValue
        }
    }
}
let someClass = SomeClass()

someClass.id = "A"
print(someClass.id)  // A


2 ) Getters: internal, Setters: private

๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๋Š” Getters ๋Š” internal ์˜ Access Levels ๋ฅผ ๊ฐ–๊ณ , Setters ๋Š” private ์˜ Access Levels ๋ฅผ ๊ฐ–๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

class SomeClass {
    private var _id: String = ""
    var id: String {
        get {
            _id
        }
    }
    func setId(_ id: String) {
        self._id = id
    }
}
let someClass = SomeClass()

someClass.id = "A"   // error: cannot assign to property: 'id' is a get-only property
someClass.setId("A")
print(someClass.id)  // A

Getters ๋Š” internal ์˜ Access Levels ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, Setters ๋Š” private ์˜ Access Levels ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด setId(_:)๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•œ ์ ‘๊ทผ๋งŒ ํ—ˆ์šฉ๋œ๋‹ค.


3 ) internal private(set)

Swift ๋Š” ์œ„์™€ ๊ฐ™์ด ์ž‘๋™๋˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ Access Levels ๋ฅผ ๊ฐ–๋Š” Properties ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

class SomeClass {
    internal private(set) var id: String = ""

    func setId(_ id: String) {
        self.id = id
    }
}

internal Variables ์— Setters ์˜ Access Levels ๋ฅผ private์œผ๋กœ ์„ ์–ธํ•˜๋ฉด Swift ๊ฐ€ ์ด์— ๋งž๊ฒŒ Compile ํ•œ๋‹ค.
(internal Variables ์˜ Getters ๋Š” internal ์ด๋‹ค.)

let someClass = SomeClass()

someClass.id = "A"   // error: cannot assign to property: 'id' is a get-only property
someClass.setId("A")
print(someClass.id)  // A


4 ) private(set)

๊ทธ๋Ÿฐ๋ฐ SomeClass Types ์˜ Access Levels ๊ฐ€ internal ์ด๋‹ค.

์•ž์—์„œ ์„ค๋ช…ํ–ˆ๋“ฏ์ด Types ์˜ Access Levels ๊ฐ€ Open ๋˜๋Š” Public ์ผ ๊ฒฝ์šฐ Properties ๊ฐ€ ์•”์‹œ์ ์œผ๋กœ Internal ์ด ๋˜์ง€๋งŒ Types ์˜ Access Levels ๊ฐ€ Internal ์ดํ•˜์ผ ๊ฒฝ์šฐ Properties ๋Š” ์•”์‹œ์ ์œผ๋กœ Types ์˜ Access Levels ๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ Properties ๊ฐ€ ์•”์‹œ์ ์œผ๋กœ Types ์˜ Access Levels ๋ฅผ ๋ฐ›๋„๋ก ์ƒ๋žตํ•˜๊ณ  Setters ์˜ Access Levels ๋งŒ ์ง€์ •ํ•ด ์งง์€ ํ˜•ํƒœ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

internal private(set) -> private(set)

class SomeClass {
    private(set) var id: String = ""

    func setId(_ id: String) {
        self.id = id
    }
}
let someClass = SomeClass()

someClass.id = "A"   // error: cannot assign to property: 'id' is a get-only property
someClass.setId("A")
print(someClass.id)  // A

๊ฒฐ๊ตญ Stored Properties ๋Š” Backing Storage ์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ Access Levels ์— ๋”ฐ๋ผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด Computed Properties ์™€ ์œ ์‚ฌํ•œ ํ˜•ํƒœ์˜ ๊ตฌํ˜„์„ ์•”์‹œ์ ์œผ๋กœ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, Access Levels ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” Getters์™€ Setters๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๊ตฌํ˜„์„ ํ•˜๋“  ์•”์‹œ์ ์œผ๋กœ ๊ตฌํ˜„์ด ๋˜๋“  Stored Properties ์™€ Computed Properties ๋ชจ๋‘์— ์ ์šฉ๋œ๋‹ค.


๋‹ค์Œ ์˜ˆ์ œ๋Š” โ€˜valueโ€™ ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ๋ณ€๊ฒฝ ํšŸ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•˜๋Š” โ€˜numberOfEditsโ€™ ๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋„๋ก โ€˜valueโ€™ ์ž์‹ ์—๊ฒŒ Observer ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , numberOfEdits ๋Š” ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋„๋ก private(set)์„ ์ด์šฉํ•ด ์ •์˜ํ•˜์˜€๋‹ค.

struct TrackedString {
    private(set) var numberOfEdits = 0
    var value: String = "" {
        didSet {
            numberOfEdits += 1
        }
    }
}
var tracking = TrackedString()
print(tracking.numberOfEdits)   // 0
tracking.value = "A"
print(tracking.numberOfEdits)   // 1
tracking.value = "B"
print(tracking.numberOfEdits)   // 2
tracking.value += "C"
print(tracking.numberOfEdits)   // 3

8. Initializers ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Initializers

  • Custom Initializers ๋Š” Types ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.
  • ๋‹จ, Required Initializers ๋Š” Types ๋Š” ์ž์‹ ์ด ์†ํ•œ Class ์™€ ๋™์ผํ•œ Access Level ์„ ๊ฐ€์ ธ์•ผํ•œ๋‹ค.
  • Functions ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Parameters ๋ณด๋‹ค ๋†’์€ ์ˆ˜์ค€์˜ Access Levels ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค. (e.g. Guiding Principle of Access Levels, Function Types)

Access Levels

2. Default Initializers

Default Initializers ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ Access Levels ๋ฅผ ๊ฐ–๋Š”๋‹ค.

Access Levels

  • (internal, fileprivate, private) Default Initializers == Types
  • (open, public) Default Initializers = internal

(open, public) Types ์— ์˜ํ•ด ์ƒ์„ฑ๋˜๋Š” Default Initializers ๋Š” internal์ด๋‹ค. ๋”ฐ๋ผ์„œ ์™ธ๋ถ€ ๋ชจ๋“ˆ์— arguments ๊ฐ€ ์—†๋Š” (open, public) Initializers ๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ no-argument Initializer๋ฅผ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.

3. Default Memberwise Initializers for Structure

Access Levels

  • Structures ์˜ ๋ชจ๋“  Stored Properties ๊ฐ€ private -> Default Memberwise Initializers ๋Š” private
  • Structures ์˜ ๋ชจ๋“  Stored Properties ๊ฐ€ fileprivate -> Default Memberwise Initializers ๋Š” fileprivate
  • ๊ทธ ์™ธ -> Default Memberwise Initializers ๋Š” internal

Default Initializers ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์™ธ๋ถ€ ๋ชจ๋“ˆ์— Memberwise Initializers ๋ฅผ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ Public Memberwise Initializers๋ฅผ ์ •์˜ํ•ด์•ผํ•œ๋‹ค.


9. Protocols ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Protocols

  • Protocols ์˜ ๊ธฐ๋ณธ Access Levels ๋Š” internal ์ด๋‹ค.
  • Protocols ์˜ Types ์— ๋ช…์‹œ์ ์œผ๋กœ Access Levels ๋ฅผ ์ œํ•œํ•ด ํŠน์ • context ๋‚ด์—์„œ๋งŒ ์ฑ„ํƒ(adoption)๋  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

Access Levels

  • Requirements = Protocols
  • Requirements ์˜ Access Levels ๋ฅผ Protocols ์™€ ๋‹ค๋ฅด๊ฒŒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
  • ๋‹ค๋ฅธ Types ์™€ ๋‹ค๋ฅด๊ฒŒ Protocols ๊ฐ€ (open, public) ์ผ ๋•Œ Requirements ์—ญ์‹œ ๋™์ผํ•œ (open, public) Access Levels ๋ฅผ ๊ฐ–๋Š”๋‹ค.

2. Protocol Inheritance

Access Levels

  • Sub Protocols โ‰ค Super Protocols

3. Protocol Conformance

Access Levels

  • Protocols โ‰ค Types
  • Requirements = min(Types, Protocols)
protocol SomeProtocol {
    var protocolProperty: Int { get }
}

protocol SomePrivateProtocol {
    var privateProtocolProperty: Int { get }
}

struct SomeStructure: SomeProtocol, SomePrivateProtocol {
    var protocolProperty: Int
    var privateProtocolProperty: Int
}

var some = SomeStructure(protocolProperty: 10, privateProtocolProperty: 30)
print(some.protocolProperty)  // 10
some.protocolProperty = 5
print(some.protocolProperty)  // 5

print(some.privateProtocolProperty) // 30
some.privateProtocolProperty = 50
print(some.privateProtocolProperty) // 50

Setters ๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Protocols ๊ฐ€ private ์ด์–ด๋„ ์‹ค์ œ Requirements ๋Š” fileprivate ๊นŒ์ง€๋Š” ํ—ˆ์šฉ์ด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

Objective-C ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Protocols ์˜ Conformance๋Š” Global ์ด๋‹ค. ํ•œ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ Protocol ์„ ์ค€์ˆ˜ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


10. Extensions ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Extensions

Classes, Structures, Enumerations ๋ฅผ ํ™•์žฅํ•˜๋ฉด ๊ธฐ์กด Types ์˜ Members ๊ฐ€ ๊ฐ–๋Š” default Access Levels ๋ฅผ ๋™์ผํ•˜๊ฒŒ ๊ฐ–๋Š”๋‹ค.
Extensions ์— Access Levels ๋ฅผ ์ •์˜ํ•˜๋ฉด, Extensions ์— ์˜ํ•ด ์ถ”๊ฐ€๋˜๋Š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ์•”์‹œ์ ์œผ๋กœ ์ •์˜๋˜๋Š” Access Levels ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

Access Levels

  • Extensions โ‰ค Types
  • (open, public) Types ๋ฅผ Extensions -> internal Members
  • (internal, fileprivate, private) Types ๋ฅผ Extensions -> (internal, fileprivate, private) Members
struct SomeStruct {
    var number: Int
    func double() -> Int { self.number * 2 }
}

private extension SomeStruct {
    func triple() -> Int { self.number * 3 }
}


var some = SomeStruct(number: 5)
some.number     // 5
some.double()   // 10
some.triple()   // 15

Extensions ๋ฅผ private ์œผ๋กœ ์ •์˜ํ•˜๋ฉด, Extensions ์— ์˜ํ•ด ์ถ”๊ฐ€๋˜๋Š” ๊ธฐ๋Šฅ์€ private ์œผ๋กœ ์ •์˜๋œ๋‹ค(๋ฌผ๋ก  ์œ„ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ fileprivate ์€ ํ—ˆ์šฉ์ด ๋˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค).

2. Private Members in Extensions

Extensions ์ด Classes, Structures, Enumerations ์™€ ๊ฐ™์€ ํŒŒ์ผ์— ์กด์žฌํ•  ๊ฒฝ์šฐ, Original ๊ณผ Extensions ๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹จ์ผ Original Types ์— ์ •์˜๋œ ๊ฒƒ์ฒ˜๋Ÿผ ์ž‘๋™ํ•œ๋‹ค.

struct Origin {
    private let originNumber = 5
    func printExtensionNumber() { print(doubleNumber) }
}

extension Origin {
    private var doubleNumber: Int { originNumber * 2 }
    func printAnotherExtensionNumber() { print(tripleNumber) }
}

extension Origin {
    private var tripleNumber: Int { originNumber * 3 }
    func printOriginNumber() { print(originNumber) }
}

var someStructure = Origin()
someStructure.printExtensionNumber()        // 10
someStructure.printAnotherExtensionNumber() // 15
someStructure.printOriginNumber()           // 5

11. Generics ๐Ÿ‘ฉโ€๐Ÿ’ป

Generic Types ๋˜๋Š” Generic Functions ์˜ Access Levels ๋Š” ์ž๊ธฐ ์ž์‹  ๋˜๋Š” Type Parameters ์˜ Constraints ์ค‘ ์ตœ์†Ÿ๊ฐ’์œผ๋กœ ์ •ํ•ด์ง„๋‹ค.

Access Levels

Generic Types, Generic Functions = min(itself, Type Parameters)


12. Type Aliases ๐Ÿ‘ฉโ€๐Ÿ’ป

Type Aliases ์—ญ์‹œ Swift ์˜ ๋‹ค๋ฅธ Types ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ณ ์œ ํ•œ Types ๊ฐ€ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ Type Aliases ๋ฅผ ์‚ฌ์šฉํ•ด ๊ธฐ์กด Types ์˜ Access Levels ๋ฅผ Original ๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ๋‚ฎ๊ฒŒ ๋ณ€๊ฒฝํ•ด ๊ณ ์œ ์˜ Types ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

Access Levels

Type Aliases โ‰ค Types

struct SomeStruct {
    var number: Int
    func double() -> Int { self.number * 2 }
}

private typealias PrivateStruct = SomeStruct
public typealias PublicStruct = SomeStruct  // Type alias cannot be declared public because its underlying

Original Types ๊ฐ€ internal ์ด๊ธฐ ๋•Œ๋ฌธ์— public ์œผ๋กœ Access Levels ๋ฅผ ๋” ๊ฐœ๋ฐฉํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

var privateStruct = PrivateStruct(number: 5) // error: variable must be declared private or fileprivate because its type 'PrivateStruct' (aka 'SomeStruct') uses a private type

Private Types ์ด๋ฏ€๋กœ Internal Variables ์— ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.

private var privateStruct = PrivateStruct(number: 5)
privateStruct.number    // 5
privateStruct.double()  // 10

์ด Rule ์€ Protocols ์˜ ์ค€์ˆ˜์„ฑ(conformances)๋ฅผ ์ถฉ์กฑํ•˜๋„๋ก ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” Associated Types ์—๋„ ์ ์šฉ๋œ๋‹ค.




Reference

  1. โ€œAccess Control.โ€ The Swift Programming Language Swift 5.7. accessed Mar. 29, 2023, Swift Docs Chapter 26 - Access Control.