Access Control - The Swift Programming Language (Swift 5.5)

접근 제어란? 코드끼리 상호 작용을 할 때, 파일과 모듈 간 접근을 제한할 수 있는 기능

fileprivate

.swift파일 내부에서만 접근 가능하도록 하는 접근 제어자.

//  Sample.swift

// MARK: - Constants
fileprivate let defaultBackgroundColorTop = UIColor(red: 0.263, green: 0.118, blue: 0.565, alpha: 1.00)
fileprivate let defaultBackgroundColorBottom = UIColor(red: 1.000, green: 0.357, blue: 0.525, alpha: 1.00)

class SunAndMoonExampleViewController: UIViewController {
...
}

fileprivate let으로 선언된 변수들은 'Sample.swift' 내부에서만 접근할 수 있다.

만약 fileprivate 키워드가 붙어있지 않다면, 외부의 다른 파일들에서도 해당 변수들을 접근할 수 있게 된다.

private sample: singleton

static, private init, shared를 통해 사용.

class UserInfo {
    static let shared = UserInfo()

    var id: String?
    var password: String?
    var name: String?

    private init() { }
}

//A ViewController
let userInfo = UserInfo.shared
userInfo.id = "Sodeul"
//B ViewController
let userInfo = UserInfo.shared
userInfo.password = "123"

언제 사용되나 ?

let screen = UIScreen.main

let userDefault = UserDefaults.standard