ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Swift Language ) Access Control
    Swift/Swift Language 2021. 9. 16. 14:59

    Swift 에서 Access Control 은 소스코드 간의 접근을 관리하는 거에요.

    Access Control 을 통해 detail 한 implementation 을 숨길 수 있고, protocol 에 대해서도 정해줄 수 있어요.

    각 individual types (classes, structures, and enumerations), 이에 속하는

    properties, methods, initializers, subscripts  에 대해 access level 을 지정해줄 수 있어요. (어디까지 허용할지)

     

    Swift 에서는 default access level 을 제공하기 때문에, single-target app 에 대해서는 따로 지정해주지 않아도 될거에요.

     

    ( properties, types, functions, 그 외 access conttrol 을 가질 수 있는 것들에 대해 지금부터 entities 라고 부를게요)

     

    Modules and Source Files 

    Swift 에서 access control model 은 modules 과 souce files 를 기반으로 합니다. 

     

    module 은 하나의 code distribution 단위에요. single unit 으로 built, shipped 될 수 있는 framework or application 이고, 따라서 Swift 에서 import 키워드를 이용하여 imported 될 수 있습니다. 

    (A module is a single unit of code distribution—a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s importkeyword.)

     

    Xcode 내에서 각각의 build target (app bundle or framework) 은 분리된 모듈로 처리돼요. 

    다른 앱에서도 사용하기 위해 만약 하나의 framework로 app 내 code 를 묶는다면 then everything you define within that framework will be part of a separate module when it’s imported and used within an app, or when it’s used within another framework.

     

    Although it’s common to define individual types in separate source files, a single source file can contain definitions for multiple types, functions, and so on.

     

    하나의 소스 파일은 모듈 내에서(within an app or framework) 하나의 Swift source code file이에요 . 

    많은 경우 individual types 을 각각의 소스파일에서 정의하지만, 

    하나의 소스코드는 여러개의 types, functions 등에 대한 정의를 포함할 수 있어요. 

     

    Access Levels

    // Swift 는 entities 에 대하여 5가지의 AccessLevel 을 갖습니다. 이 Access Level 은 entity 가 정의되어있는 소스코드, 그리고 소스코드가 있는 module 에 관련있습니다.

    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, ( highest level, least restriction)

    public access ( 

    속해있는 module 내에 있는 모든 source file 에서, 그 module 을 import 하는 source file 에서 접근 가능합니다. 

    보통 framework 에서 public interface 를 지정할 때 많이 쓰여요. 

     

    Internal access: 

    속해있는 module 내에 있는 모든 source file 에서만 접근 가능합니다. ( 그 외는 안돼요!) 내부 구조를 짜는 데에 주로 사용됩니다.

     

    File-private access: 

    정의되어있는 source file 내에서만 접근 가능합니다. implementation details 를 숨길 때 사용합니다. 

     

    private access: (lowest level, highest restriction)

    둘러싸고 있는 코드 내에서, 같은 file 내 extension 을 이용한 곳에서만 접근 가능합니다. implementation details 이 single declaration 내에서만 사용되는 경우 이때 implementation details 를 숨기고 싶을 때 사용합니다. 

     

    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.

     

    Guiding Principle of Access Levels

    Swift AccessLevel 은 다음 가이드라인을 따릅니다 :

    높은 access Level 의 entity 는 더 낮은 level 의 entiy 의 definition 에 속해있을 수 없다. 

    For example: 

    • A public variable can’t be defined as having an internal, file-private, or private type, because the type might not be available everywhere that the public variable is used.
    • A function can’t have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are unavailable to the surrounding code.

    Default Access Levels

    Default Access Levels 는 모든 entities 에 대하여 internal Level 입니다. (속해있는 module 내에 있는 모든 source file 내에서만 접근 가능.) 

     

    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
    }

     

    출처: https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

    'Swift > Swift Language' 카테고리의 다른 글

    Swift Language ) Closure  (0) 2021.10.06
    Swift Language) ARC ( Automatic Reference Counting)  (0) 2021.10.04
    Swift Language ) Generics  (0) 2021.09.23
    Swift Language ) Protocol  (0) 2021.09.16
    Swift Language ) Extensions  (0) 2021.09.14
Designed by Tistory.