Access Control
๋ง ๊ทธ๋๋ก ์ฝ๋์ ์ ๊ทผ์ ์ ์ดํ๋ ๊ธฐ๋ฅ์ด๋ค.
๊ฐ๋ณ ํ์ (individual types)(ํด๋์ค, ๊ตฌ์กฐ์ฒด ๋ฐ ์ด๊ฑฐ) ๋ฟ๋ง์๋๋ผ, ํด๋น ํ์ ์ ์ํ๋ ํ๋กํผํฐ, ๋ฉ์๋, ์ด๋์ ๋ผ์ด์ ๋ฐ ์ฒจ์(subscripts)์ ๋ํด ํน์ ์ ๊ทผ ๋ ๋ฒจ์ ์ง์ ํ ์ ์๋ค.
ํ๋กํ ์ฝ์ ์ ์ญ ์์, ๋ณ์ ๋ฐ ํจ์์ฒ๋ผ ํน์ ์ปจํ ์คํธ๋ก ์ ํ๋๋ค.
์ ๊ทผ ์ ์ด๋ modules๊ณผ source file ๋์์ผ๋ก ์ ์ฉ๋๋ค.
swift๋ ์ํฐํฐ์ ๋ํด 5๊ฐ์ง ์ ๊ทผ ์ ์ด ๋ ๋ฒจ์ ๊ฐ์ง๋ค.
open, public
์ ์๋ ๋ชจ๋๋ด ๋ชจ๋ ์์ค ํ์ผ ๋ด์์ ์ฌ์ฉํ ์ ์๋ค.
open ์ ๊ทผ์ ํด๋์ค ๋ฐ ํด๋์ค ๋ฉค๋ฒ์๋ง ์ ์ฉ๋๋ค. struct, enum ๋ฑ์ ํ์ ์ Public์ด ๊ฐ์ฅ ์ต์์ ์ ๊ทผ ์ ์ด๋ค.
ํด๋์ค์ ํด๋์ค ๋ฉค๋ฒ์ ์ฐ์ด๋ open๊ณผ public ์ดํ ์ ๊ทผ์์ ์ฐจ์ด์
//๋ค๋ฅธ ๋ชจ๋์ ๊ฒฝ์ฐ
๋ชจ๋ 1
open class classA {
public init() {}
open func a() {}
public func b() {}
}
public class classB {
public init() {}
}
๋ชจ๋ 2
class Test {
init() {}
let aClass = ClassA() // OK
let bClass = ClassB() // OK
}
class Test: ClassA { // OK
}
class Test: ClassB { // error
}
๋๋ค ๋ค๋ฅธ ๋ชจ๋์์ import ํด์ ์จ ํด๋์ค๋ฅผ ์์ฑ ๊ฐ๋ฅ
๋ฐ๋ฉด ์์ ๋ฐ๊ณ ์ ํ ๋(์๋ธ ํด๋์ฑ), public ์ ๊ทผ์ ์ดํ(public, internal, File-private, private)๋ก ์ ์๋ ๋ค๋ฅธ ๋ชจ๋ ๋ด์ class๋ ์์์ด ๋ถ๊ฐ๋ฅํ๋ค.
๋ํ open ClassA ๋ด์ ๋ณด๋ฉด open func a()์ public func b()๊ฐ ์๋ค.
class Test: ClassA {
override func a() {} // open ์ ๊ทผ ์ ์ด ํจ์ OK
override func b() {} // public ์ ๊ทผ ์ ์ด ํจ์ error
}
ํด๋์ค ๋ฟ๋ง ์๋๋ผ ํด๋์ค ๋ฉค๋ฒ์๊ฒ ์ญ์ public ์ ๊ทผ์ ์ดํ(public, internal, File-private, private)๋ก ์ ์๋ ๋ฉค๋ฒ๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค.
//๊ฐ์ ๋ชจ๋์ ๊ฒฝ์ฐ - ๋ชจ๋ 1
open class classA {
public init() {}
}
public class classB {
public init() {}
}
class Test {
init() {}
let aClass = ClassA() // OK
let bClass = ClassB() // OK
}
class Test: ClassA { // OK
}
class Test: ClassB { // OK
}
๊ฐ์ ๋ชจ๋์ผ ๊ฒฝ์ฐ open, public ์ ๊ทผ ์ ์ด๊ฐ ์๊ด ์์ด ์๋ธ ํด๋์ฑ์ด ๊ฐ๋ฅํ๋ค.(open๊ณผ public, internal, File-private, private์ ์ฐจ์ด๊ฐ ์๋ค)
Internal
์ ์ ๋ชจ๋์ ๋ชจ๋ ์์ค ํ์ผ ๋ด์์ ์ฌ์ฉ๋์ง๋ง, ํด๋น ๋ชจ๋ ์ธ๋ถ์ ์์คํ์ผ์์๋ ์ฌ์ฉ๋์ง ์๋๋ก ์ ์ดํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก ์์ ์์ ์์
๋ชจ๋ 1
open class classA {
public init() {}
public func a() {}
public func b() {}
}
internal class classB {
public init() {}
}
private class classC {
public init() {}
private funcC() {}
private varC = ""
}
๋ชจ๋ 2
class Test {
init() {}
let aClass = ClassA() // OK
let bClass = ClassB() // error
}
internal์ ๋ค๋ฅธ ๋ชจ๋์์ ์ ๊ทผ ์์ฒด๊ฐ ๋ถ๊ฐ๋ฅํ๋ค
๋์ฑ์ด ๋ฐ๋ก ํ์ ์ ๋ช ์ํ์ง ์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก internal ์ ๊ทผ ์ ์ด๋ฅผ ์ฑํํ๊ณ ์๋ค.
File-private
์ด๋ฆ์์ ์ ์ ์๋ค ์ํผ, ๊ฐ์ ์์ค ํ์ผ ๋ด๋ก ์ ๊ทผ ์ ์ด๋ฅผ ํ๋ค.
๊ฐ์ ๋ชจ๋ ๋ด, ๊ฐ์ ํ์ผ์๋ง ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
Private
์ ์ธ๋ ํด๋์ค ๋ด์์๋ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
๋ชจ๋ 1
open class classA {
public init() {}
public func a() {}
public func b() {}
}
internal class classB {
public init() {}
}
private class classC {
public init() {}
private funcC() {}
private varC = ""
}
class Test {
let cClass = classC() // OK
cClass.funcC() // error
print(cClass.varC) //error
}
์์ฑ์๋ public ํ์ ์ด๋ผ ์์ฑ์ด ๊ฐ๋ฅํ์ง๋ง ์ด๋ฐ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. classC๊ฐ private์ด๋ฏ๋ก ๋ณ์๋ฅผ fileprivate(์ ์ธ์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค)๋ private๋ก ๋ฐ์ ์ค์ผ ํ๋ค.
ํ์ง๋ง extension ์ ์ฌ์ฉํ ๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
์ฐธ๊ณ :
Access Control — The Swift Programming Language (Swift 5.6)
Access Control Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be
docs.swift.org
'IOS๐ > iOS+Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Opaque Type์ด๋ (0) | 2022.04.12 |
---|---|
[iOS] Swift Unit Test ์ดํด๋ณด๊ธฐ (0) | 2022.04.07 |
[Swift] CustomStringConvertible (0) | 2022.03.07 |
[iOS/swift] ์ ์ฉํ ์ฌ์ดํธ ์ ๋ฆฌ ๋ชจ์.zip (0) | 2021.07.03 |
[iOS] Codable - Encodable & Decodable ํ๋กํ ์ฝ (0) | 2021.05.31 |
๋๊ธ