• No results found

You specify access control levels by preceding the entity to which they refer with one of the keywords, public, internal, or private, as in the following:

public let APIVersion = "1.0.0" private struct HashingRecord { }

internal class Byte { }

The access level specified for a type affects the default access level for its members:

• If a type’s access is marked as private, its members will default to private.

• If a type’s access is marked as public or internal, its mem‐ bers will default to internal—you need to explicitly declare members as public if you want them visible out‐ side the current module, even if the containing entity itself is marked as public.

• If a type’s access is not specified, it, and its members, will default to internal.

In the earlier section on classes a Byte class was introduced to

demonstrate the use of the subscript function, which provided

access to Byte instances as though they were an array of bits. If

this class was to be included in a framework for other programs to use, it would be necessary to make the class and its subscript member public, but it would also be reasonable to mark the variable that stores the byte value as private, so the implemen‐ tation is opaque to callers, as in this example:

public class Byte

{

private var d: UInt8 = 0

// rest of subscript definition }

}

Other code that imported this library module would be able to create instances of the Byte class, and could set and get their

value by subscript, but could not access the d property directly.

Default Access Control Levels

Although the default access level for most entities is internal, there are some exceptions and caveats you might need to con‐ sider, which are presented in Table 6.

Table 6. Access control restrictions

Type Default/available access level(s) Constants,

variables, properties

Constants, variables, and properties must have either the same access level as their type, or a more restrictive level. For example, a variable of type SomeClass that is marked internal

cannot itself be marked as public. Enumeration

cases The access level of the cases of an enumeration is the same as theaccess level of the enumeration itself (enumeration cases are not members in the usual sense).

Enumeration

values The default access level is the same as the access level of theenumeration with which the values are associated and cannot be overridden with a more restrictive access level.

Extensions When extending a class, structure, or enumeration, new members defined in the extension have the same default access level as members of the original entity:

• You can override the default for new members by specifying a different access level on the extension. • The access level for new members can override the

default but cannot be more open than the access level of the original entity.

• You cannot specify an access level on an extension that adds protocol conformance.

Type Default/available access level(s)

Function The default access level is the most restrictive of all of the function’s parameter and return types. You can override the default but only with a more restrictive level.

Generics The effective access level for generics is the most restrictive of the access level of the generic itself as well as the access level of any of its constraining types.

Getters,

setters The default access level for getters and setters is the same as theaccess level for the entity on which they are defined. The setter can have a more restrictive level than the getter (limiting modification of the entity without affecting its readability), which is specified by preceding the variable or property name with either private(set) or internal(set). Initializers The default access level for initializers is the same as the access

level of the class to which they belong. You can override the default for a custom initializer with a more restrictive level. Nested types • Types defined within private types will also be private.

• Types defined within internal types will default to the internal access level but can be made private. • Types defined within public types will default to the

internal access level, and you can override with any access level.

Protocols The default access level is internal, but you can override this with any level. Each requirement within the protocol has the same access level as the protocol itself, and this cannot be overridden. Subclasses The default access level is the same as that of the superclass, but you can override this with a more restrictive level. A subclass can override the implementation of an inherited class member and override that member’s access level as long as it is not more open than the access level of the subclass.

Subscripts The default access level for a subscript is the most restrictive of its index and return types. You can override this only with a more restrictive level.

Type Default/available access level(s)

Tuple The only available access level is the most restrictive level of all of the types that comprise the tuple. You cannot override this because tuples are not explicitly defined like other types. Type aliases The default access level for a type alias is the same as that of the

type that it aliases. You can override the default with a more restrictive level.

Related documents