Swift 3: The difference between Public and Internal access modifiers? [duplicate]
Asked Answered
D

6

18

I read the Apple's reference about access modifiers in Swift 3. I read also about the same on stackoverflow but I didn't get an answer as the person who asked. As I understood correctly, there are four levels:

  1. Open, Public
  2. Internal
  3. Fileprivate
  4. Private

I created the schemes for myself to understand a difference between all these modifiers and uploaded here. As you can see, there are no differences between Public and Internal modifiers.. However they're on different levels. Any idea would be appreciated!

Dunaville answered 20/3, 2017 at 7:3 Comment(4)
for FilePrivate and Private different read this blog , you can easily understandCorriveau
@SunilPrajapati thank you. The third and fourth levels are clear. The question was only about Open and Public modifiers.Dunaville
read answer with example Click Here and Second LinkCorriveau
Swift Access ControlStent
O
2

Your diagram is just incorrect.

Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.

Oeflein answered 20/3, 2017 at 7:23 Comment(1)
It doesn't seem to me, as beginner, evident however currently I feel the difference. Thank you!Dunaville
H
28
  • Internal - This is default access specifier in swift. With this we can access data members and member functions in the same module (target).

  • Public - This is where you can access all data members and member functions within same module and outside of it. But you can't subclass or override outside the module.

  • Open - same as public, only difference is you can subclass or override outside the module.

  • Fileprivate - As the name say's, data members and member functions are accessible within the same file.

  • Private - This is where you can have access within the scope of function body or class.

Holoenzyme answered 20/12, 2018 at 15:20 Comment(3)
(Swift 4+) Private: also access within extensionsArgumentative
private is not access within extensions .Courthouse
Private: also accessible within extensions but in same file onlyMentholated
U
10

Whatever you marked as public can be used within your app and outside of your app(module). If you marked something as internal that can only be used within your app(module). This is very helpful when you're developing a library (framework), you can use internal to hide library structure.

Ushas answered 20/3, 2017 at 7:50 Comment(1)
Good thought. It makes me closer to understanding. Thank you.Dunaville
H
8

The The Swift Programming Language book from Apple clearly explains these access modifiers:

“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. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. https://itun.es/gb/jEUH0.l

Hexachord answered 8/8, 2017 at 9:4 Comment(0)
H
7

Swift access modifiers

Depends on access modifier of class, function or property it can be subclassed, overrode, accessible

Access modifier can be applicable for class, field[About], method. Try to access, subclass or override this.

  • Access to field or method is through a class
  • Inheritance and Open Closed Principle[About]
    • Extension, wrapper
    • Successor class(subclass) access modifier should be the same or restrict it(except private <-> fileprivate).
    • Successor method(override) access modifier should be the same or expand it

[Java access modifiers]

Hero answered 28/3, 2020 at 18:35 Comment(0)
C
3
•   Public - Can be used from any module but can’t be subclassed outside defining module (target).

•   Internal - This is default access modifier in swift. Can be accessible from the defining module (target) only.

•   Open - Can be used from any module and can be subclassed outside defining module (target).

•   Swift 4+

•   Fileprivate - Fileprivate members and functions are accessible within the same file, within the extension in same file

•   Private - Private members and functions are accessible within the same file, within the extension in same file.
Capreolate answered 6/11, 2019 at 13:47 Comment(0)
O
2

Your diagram is just incorrect.

Public members of A.swift and B.swift are available to C.swift and D.swift. The only restriction is that classes can't be subclassed (they would need to be open.

Oeflein answered 20/3, 2017 at 7:23 Comment(1)
It doesn't seem to me, as beginner, evident however currently I feel the difference. Thank you!Dunaville

© 2022 - 2025 — McMap. All rights reserved.