UICollectionView에 대해서 공부하던 중에,

https://theswiftdev.com/ultimate-uicollectionview-guide-with-ios-examples-written-in-swift/

 

위 포스트를 보게 되었고, 이분이 만든 CollectionView 프레임워크를 분석해보자고 마음 먹었다.

사실.. CollectionView 사용법이 나한테는 생각보다 어렵게 느껴져서,

이분이 만든 프레임워크와 예제를 분석하다 보면, 쉽게 이해될 수도 있을 것 같다는 조금의 희망을 가지고 시작한거다.

겸사겸사 swift 공부도 해볼겸..

 

그러다가, 아래와 같은 코드를 만나게 되었는데,

open 이라는 키워드를 처음 본 것 같아서, 이참에 알아봐야겠다 싶었다.

open class CollectionView: UICollectionView {

    open var source: Source? = nil {
        didSet {
            self.source?.register(itemsFor: self)

            self.dataSource = self.source
            self.delegate = self.source
        }
    }
}

 

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3

Swift 문서에서 이런걸 찾게 되었다.

Access Levels

Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.

  • Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below.
  • Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
  • File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
  • Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level.

Open access applies only to classes and class members, and it differs from public access by allowing code outside the module to subclass and override, as discussed below in Subclassing. Marking a class as open explicitly indicates that you’ve considered the impact of code from other modules using that class as a superclass, and that you’ve designed your class’s code accordingly.

 

----------

 

Swift에는 5가지 접근 레벨이 있다.

5가지 레벨에는 open, public, internal access, file-private, private 가 있다.

 

위의 영어로 entities라고 있는데, 엔티티프로퍼티, 타입, 함수 등등을 이야기 한다고 합니다.

모듈(Module)은 쉽게 프레임워크라고 생각하면 좋을 것 같다.

 

open

- class에와 class member에만 사용할 수 있다.

- 모든 곳에서 sub classing이 가능하다.

 

public

- 클래스에 붙어있는 경우, 모듈 내에서만 sub classing이 가능하다.

 

Internal access

- 모듈 안에서 사용 가능

 

File-private

- 소스 파일 안에서만 사용 가능

 

private

- 소스 파일 안, 해당 선언 안에서만 사용 가능

+ Recent posts