What is the 'open' keyword in Swift?
open
is a new access level in Swift 3, introduced with the implementationof
It is available with the Swift 3 snapshot from August 7, 2016, and with Xcode 8 beta 6.
In short:
- An
open
class is accessible and subclassable outside of thedefining module. Anopen
class member is accessible andoverridable outside of the defining module. - A
public
class is accessible but not subclassable outside of thedefining module. Apublic
class member is accessible butnot overridable outside of the defining module.
So open
is what public
used to be in previousSwift releases and the access of public
has been restricted.Or, as Chris Lattner puts it in SE-0177: Allow distinguishing between public access and public overridability:
“open” is now simply “more public than public”, providing a very simple and clean model.
In your example, open var hashValue
is a property which is accessible and can be overridden in NSObject
subclasses.
For more examples and details, have a look at SE-0117.
Read open as
open for inheritance in other modules
I repeat open for inheritance in other modules.So an open class is open for subclassing in other modules that include the defining module. Open vars and functions are open for overriding in other modules. Its the least restrictive access level. It is as good as public access except that something that is public is closed for inheritance in other modules.
From Apple Docs:
Open access applies only to classes and class members, and it differs from public access as follows:
Classes with public access, or any more restrictive access level, canbe subclassed only within the module where they’re defined.
Class members with public access, or any more restrictive access level, canbe overridden by subclasses only within the module where they’redefined.
Open classes can be subclassed within the module where they’re defined, and within any module that imports the module wherethey’re defined.
Open class members can be overridden by subclasseswithin the module where they’re defined, and within any module thatimports the module where they’re defined.
Open is an access level, was introduced to impose limitations on class inheritance on Swift.
This means that the open access level can only be applied to classes and class members.
In Classes
An open class can be subclassed in the module it is defined in and in modules that import the module in which the class is defined.
In Class members
The same applies to class members. An open method can be overridden by subclasses in the module it is defined in and in modules that import the module in which the method is defined.
THE NEED FOR THIS UPDATE
Some classes of libraries and frameworks are not designed to be subclassed and doing so may result in unexpected behavior. Native Apple library also won't allow overriding the same methods and classes,
So after this addition they will apply public and private access levels accordingly.
For more details have look at Apple Documentation on Access Control