Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protected Access Level in Swift

Tags:

swift

How can I make protected (like in ruby) variable or function in Swift? I know Swift has only 3 levels but nonetheless is it possible?

Access Levels

Swift provides three 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.

  • Public access enables 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 public access when specifying the public interface to a framework.
  • 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.
  • Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.

Public access is the highest (least restrictive) access level and private access is the lowest (or most restrictive) access level

Currently I see only one solution - write parent class with private modifier and children class in single file but it's kind of painful.

like image 488
Arsen Avatar asked Sep 16 '15 08:09

Arsen


People also ask

What is private access level in Swift?

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.

What is protected in Swift?

According to the documentation, Swift access control modifies ( open , public , internal , fileprivate , and private ) restrict access to parts of your code from code in other source files and modules. That has been overwhelming reason why there is no protected access modifier such as in other languages.

What is default access level in Swift?

internal is the default access level, and makes a declaration visible within the whole module that it's defined in. public reveals a function, type, extension or property outside of its module. open enables a class to be subclassed, and a function or property to be overridden, outside of its module.


2 Answers

Swift prefers to not use protected. You can read the reasons here Access Control and protected

In contrast, protected conflates access with inheritance, adding an entirely new control axis to reason about. It doesn’t actually offer any real protection, since a subclass can always expose “protected” API through a new public method or property. It doesn’t offer additional optimization opportunities either, since new overrides can come from anywhere. And it’s unnecessarily restrictive — it allows subclasses, but not any of the subclass’s helpers, to access something.

like image 187
onmyway133 Avatar answered Oct 10 '22 22:10

onmyway133


In Ruby's point of view, it may be important. However in Swift, neither it is useless, nor it is a matter of the language.

Swift language is primarily based on modules when it comes to access levels. It even has public private(set) variables, which is much needed in Objective-C (causes boilerplate).

like image 45
Buğra Ekuklu Avatar answered Oct 10 '22 23:10

Buğra Ekuklu