I want to create a class that inherits from another class, which is in a different file.
For example:
Class1.swift
class Class1 { protected var //Do Stuff }
Class2.swift
class Class2:Class1 { //Do stuff }
How would I be able to have access to a 'protected' variable/function in swift?
When I declare a private variable/function, I can only use it in that class. If I use 'fileprivate', my other class HAS to be in the same file as Class1
. What I want to do is keep my classes in separate files and use the Groups from within Xcode to know what class belongs with which category.
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.
Access modifiers in Swift are implemented differently than other languages. There are three levels: private : accessible only within that particular file. internal : accessible only within the module (project) public : accessible from anywhere.
Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.
Declaring VariablesYou begin a variable declaration with the var keyword followed by the name of the variable. Next, you add a colon, followed by the variable type. Afterward, you can assign a value to the variable using the (=) assignment operator.
You would have to use internal
for that as Swift doesn't offer a protected
keyword (unlike many other programming languages). internal
is the only access modifier between fileprivate
and public
:
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.
There is a blog post that explains a little bit more about why the language designers chose not to offer a protected
keyword (or anything equivalent).
Some of the reasons being that
It doesn’t actually offer any real protection, since a subclass can always expose “protected” API through a new public method or property.
and also the fact that protected
would cause problems when it comes to extensions, as it wouldn't be clear whether extensions should also have access to protected
properties or not.
Even if Swift doesn't provide protected
access, still we can achieve similar access by using fileprivate
access control.
Only thing we need to keep in mind that we need to declare all the children in same file as parent declared in.
Animal.swift
import Foundation class Animal { fileprivate var protectedVar: Int = 0 } class Dog: Animal { func doSomething() { protectedVar = 1 } }
OtherFile.swift
let dog = Dog() dog.doSomething()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With