I want to create a class that inherits from another class, which is in a diffrent file.
For example:
Class1.swift
class Class1
{
protected var
//Do Stuff
}
Class2.swift
class Class2:Class1
{
//Do stuff
}
How would I beable to have accsess 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 save file as Class1. What I want to do is keep my classes in seporate files and use the Groups from within Xcode to know what class belongs with which category.
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.