Access Levels

Link tham khảo: https://stackoverflow.com/a/24012515arrow-up-right

`open` và `public`

Cả 2 keywords trên đều cho phép chúng ta truy cập nó từ bên trong target nơi nó được define (define module) và cả bên ngoài - những module khác.

  • open: classes and class members can be subclassed and overridden both within and outside the defining module (target).

// First.framework – A.swift

open class A {}
// Second.framework – C.swift

import First

internal class C: A {} // ok
  • public: classes and class members can only be subclassed and overridden within the defining module (target). Allow members from other modules to use them, but NOT to override them

// First.framework – B.swift

public class B: A {} // ok
// Second.framework – D.swift

import First

internal class D: B {} // error: B cannot be subclassed

`internal` - default access level

Chỉ cho phép chúng ta truy cập nó từ bên trong target nơi nó đực define (define module). Là default level, nếu không khai báo access level => mặc định là internal

`fileprivate`

Chỉ được truy cập trong cùng 1 file code.

`private`

Chỉ được truy cập trong cùng 1 class (bao gồm extensions trong cùng 1 file với class đó)

Extensions khác file cũng không truy cập được

Last updated