# Access Control

![](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2FhmZwKzSnaOC8EG9OVqHT%2Faccess%20control.png?alt=media\&token=ec13079b-fa01-46aa-bd32-d105372612b8)

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
<mark style="color:green;">**存取控制**</mark>最重要的<mark style="color:orange;">**兩個分野**</mark>為 <mark style="color:red;">**module**</mark> 和 <mark style="color:red;">**source file**</mark>：

* <mark style="color:red;">**module**</mark>：一個 **bundle ID** 下的 <mark style="color:orange;">**app**</mark> 或一個 <mark style="color:orange;">**framework**</mark>。
* <mark style="color:red;">**source file**</mark>：就是每一個 <mark style="color:orange;">**.swift**</mark> 檔案。
{% endhint %}

{% hint style="info" %}
分 <mark style="color:orange;">**5 個層級**</mark>，最高到最低依次為：

* <mark style="color:purple;">**open**</mark> / <mark style="color:purple;">**public**</mark> / <mark style="color:purple;">**internal**</mark> / <mark style="color:purple;">**file-private**</mark> / <mark style="color:purple;">**private**</mark>
{% endhint %}

####

#### fileprivate

{% hint style="info" %}
The only reason you would use <mark style="color:purple;">**fileprivate**</mark> is when you want to access your code **within the&#x20;**<mark style="color:orange;">**same**</mark>**&#x20;file** from <mark style="color:red;">**different**</mark>**&#x20;classes or structs**. 👉 SwfitLee
{% endhint %}

```swift
// fileprivate setter
fileprivate(set) var a = 1
```

#### private

{% hint style="info" %}
<mark style="color:purple;">**extensions**</mark> have to be defined **within the&#x20;**<mark style="color:red;">**same**</mark>**&#x20;file**, <mark style="color:purple;">**private**</mark> declarations are <mark style="color:red;">**not**</mark>**&#x20;visible&#x20;**<mark style="color:orange;">**outside**</mark>**&#x20;the file**.
{% endhint %}
{% endtab %}

{% tab title="📘 手冊" %}
* Swift ⟩ [Access Control](https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html)
{% endtab %}

{% tab title="📗 參考" %}
* [x] Jerry Wang ⟩ [Access Control 教學](https://medium.com/@jerrywang0420/access-control-%E6%95%99%E5%AD%B8-swift-3-ios-4d93ee567eb0)
* [x] SwiftLee ⟩ [fileprivate vs private in Swift: The differences explained](https://www.avanderlee.com/swift/fileprivate-private-differences-explained/)
{% endtab %}

{% tab title="🗣 討論" %}
* [What's the difference of `private` and `fileprivate` class in the same file in Swift?](https://stackoverflow.com/a/62827840/5409815)
{% endtab %}
{% endtabs %}
