To add a protected static property to a class in Swift, we can make use of access control keywords.
In Swift, the access control keywords are public
, internal
, fileprivate
and private
.
To make a property protected, we can mark it as fileprivate
. Similarly, to make it static, we can use the static
keyword.
Here's an example:
main.swift75 chars4 lines
In the above example, we added a fileprivate
static property called myProtectedProperty
to the MyClass
class.
Since it's marked as fileprivate
, it's only accessible within the same file. But subclasses of MyClass
can access this property since they are in the same file.
We can access it from a subclass like this:
main.swift102 chars6 lines
Note that if we mark the property as private
, it will only be accessible within the same class, and not to the subclasses.
gistlibby LogSnag