Access Control アクセス制御

Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used. アクセス制御は、あなたのコードの各部への他のソースファイルとモジュールの中のコードからのアクセスを制限します。この特徴によってあなたは、あなたのコードの実施詳細を隠すこと、そしてそれを通してそのコードがアクセスされ使用されることができる好ましいインタフェースを指定することが可能になります。

You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types. Protocols can be restricted to a certain context, as can global constants, variables, and functions. あなたは特定のアクセス水準を個々の型(クラス、構造体、および列挙)、それだけでなくそれらの型に属しているプロパティ、メソッド、イニシャライザ、そして添え字に、個別に割り当てることができます。プロトコルは特定の前後関係に制限されることができます、グローバルな定数、変数、そして関数も同じようにされることができます。

In addition to offering various levels of access control, Swift reduces the need to specify explicit access control levels by providing default access levels for typical scenarios. Indeed, if you are writing a single-target app, you may not need to specify explicit access control levels at all. アクセス制御のいろいろな水準を提供することに加えて、スウィフトは省略時のアクセス水準を典型的なシナリオのために用意することによって明示的にアクセス制御水準を指定する必要を減らします。実際に、あなたが単一ターゲットのアプリを書いているならば、明確なアクセス制御水準を指定する必要はまったくありません。

Note 注意

The various aspects of your code that can have access control applied to them (properties, types, functions, and so on) are referred to as “entities” in the sections below, for brevity. それらに適用可能なアクセス制御を持つあなたのコードのさまざまな面(プロパティ、型、関数、その他)は、簡潔さのために、下記の節において「実在」として言及されます。

Modules and Source Files モジュールとソースファイル

Swift’s access control model is based on the concept of modules and source files. スウィフトのアクセス制御モデルは、モジュールとソースファイルの概念に基づきます。

A module is a single unit of code distribution—a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s import keyword. モジュールは、コード分類の1つの単位 ― フレームワークまたはアプリケーションです、それは、単一の構成単位として構築され出荷されて、スウィフトのimportキーワードを使って別のモジュールによってインポートされることができます。

Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift. If you group together aspects of your app’s code as a stand-alone framework—perhaps to encapsulate and reuse that code across multiple applications—then everything you define within that framework will be part of a separate module when it’s imported and used within an app, or when it’s used within another framework. Xcodeでの各ビルドターゲット(例えばアプリ・バンドルまたはフレームワーク)は、スウィフトにおいて別々のモジュールとみなされます。あなたが、あなたのアプリのコードのさまざまな面をスタンドアローンのフレームワークとして ― おそらくそのコードをカプセル化して複数のアプリケーションに渡って再利用するために ― 1つにまとめるならば、あなたがそのフレームワーク内で定義するすべてのものは、それがアプリ内にインポートされて使われるとき、またはそれがもう1つのフレームワーク内で使われるとき、別々のモジュール部分になります。

A source file is a single Swift source code file within a module (in effect, a single file within an app or framework). Although it’s common to define individual types in separate source files, a single source file can contain definitions for multiple types, functions, and so on. ソースファイルは、あるモジュール内の単一のスウィフトソースコード・ファイルです(実質的には、アプリまたはフレームワーク内の単一のファイル)。通常は個々の型を別々のソースファイルに定義しますが、単一のソースファイルは、複数の型、関数、その他の定義を含むことが可能です。

Access Levels アクセス水準

Swift provides five different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to. スウィフトは、5つの異なるアクセス水準をあなたのコード内の実在に対して用意します。これらのアクセス水準は、ある実在が定義されているソースファイルと関連しています、そのうえに、ソースファイルが属しているモジュールとも関連しています。

  • Open access and public access enable entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use open or public access when specifying the public interface to a framework. The difference between open and public access is described below. 開放アクセス公開アクセス(公開用)は、実在それらが、それらの定義モジュールからのあらゆるソースファイル内で、そのうえに、定義モジュールをインポートする別のモジュールからのソースファイルの中で、使われることを可能にします。あなたは概して開放または公開アクセスを、あるフレームワークに公開インタフェースを指定するときに使います。開放と公開アクセスの間の違いは下で解説されます。
  • 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. 内部アクセス(内部用)は、実在それらが、それらの定義モジュールからのあらゆるソースファイル内で使われることを可能にします、しかし、そのモジュールの外側のいかなるソースファイルの中も除きます。あなたは概して内部アクセスを、アプリのまたはフレームワークの内部用の構造体を定義するときに使います。
  • File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file. ファイル外非公開アクセス(ファイル私用)は、ある実在の使用をそれ自身の定義ソースファイルに制限します。それら詳細があるファイル全体の内部で使われる時に、機能性のある特定部分のそれら実装詳細を隠すために、ファイル外非公開アクセスを使ってください。
  • Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration. 非公開アクセス(私用)は、ある実在の使用をそれを囲んでいる宣言に、そしてその宣言の拡張で同じファイル中のものに制限します。それら詳細がある単一の宣言内部でのみ使われる時に、機能性のある特定部分のそれら実装詳細を隠すために、非公開アクセスを使ってください。

Open access is the highest (least restrictive) access level and private access is the lowest (most restrictive) access level. 開放アクセスは最も高い(制限の最も少ない)アクセス水準であり、非公開アクセスは最も低い(制限の最も多い)アクセス水準です。

Open access applies only to classes and class members, and it differs from public access by allowing code outside the module to subclass and override, as discussed below in Subclassing. Marking a class as open explicitly indicates that you’ve considered the impact of code from other modules using that class as a superclass, and that you’ve designed your class’s code accordingly. 解放アクセスは、クラスとクラスメンバーにのみ適用されます、そしてそれは公開アクセスとは、モジュールの外側のコードにサブクラス作成およびオーバーライドをさせることによって異なります、下のサブクラスをつくるで議論されるように。あるクラスを明示的に開放とすることが指し示すのは、あなたが、そのクラスをスーパークラスとして使っている、他のモジュール由来のコードの影響を考慮に入れるということです、そしてあなたがあなたのクラスのコードをそれに従って設計するということです。

Guiding Principle of Access Levels アクセス水準の原理指針

Access levels in Swift follow an overall guiding principle: No entity can be defined in terms of another entity that has a lower (more restrictive) access level. スウィフトの中のアクセス水準は、以下の全体的な原理指針に従います:実在は、より低い(制限のより多い)アクセス水準をもつ別の実在の観点から定義されることができない。

For example: 例えば:

  • A public variable can’t be defined as having an internal, file-private, or private type, because the type might not be available everywhere that the public variable is used. 公開の変数は、内部、ファイル外非公開、または非公開の型のものを持つように定義されることができません、なぜなら、その型は公開の変数が使われるあらゆる所で利用可能ではないからです。
  • A function can’t have a higher access level than its parameter types and return type, because the function could be used in situations where its constituent types are unavailable to the surrounding code. 関数は、そのパラメータ型および戻り型より高いアクセス水準を持つことができません、なぜなら、その関数は、それの組成の型が周囲のコードに利用できない状況において使われることができるためです。

The specific implications of this guiding principle for different aspects of the language are covered in detail below. この言語の異なる面に対する、この原理指針の特定の影響は、詳細に下で扱われます。

Default Access Levels 省略時のアクセス水準

All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you don’t specify an explicit access level yourself. As a result, in many cases you don’t need to specify an explicit access level in your code. あなたのコードの中の全ての実在は(この章の後刻に記述されるように、2、3の特定の例外を除いて)、あなたが明示的に独自にアクセス水準を指定しないならば、省略時のアクセス水準である内部を持ちます。その結果、多くの場合には、あなたはあなたのコードにおいて明示的にアクセス水準を指定する必要がありません。

Access Levels for Single-Target Apps 単一ターゲットアプリのためのアクセス水準

When you write a simple single-target app, the code in your app is typically self-contained within the app and doesn’t need to be made available outside of the app’s module. The default access level of internal already matches this requirement. Therefore, you don’t need to specify a custom access level. You may, however, want to mark some parts of your code as file private or private in order to hide their implementation details from other code within the app’s module. あなたが単純な単一ターゲットアプリを書いている場合、あなたのアプリのコードは概してそのアプリの内部に自身で持っていて、そしてそのアプリのモジュールの外側で利用可能にされることは必要ありません。省略時のアクセス水準である内部は、既にこの要件に合致しています。したがって、あなたはあつらえのアクセス水準を指定する必要はありません。あなたは、しかし、あなたのコードのいくつかの部分をファイル外非公開または非公開として印して、それらの実施詳細をそのアプリのモジュール内の他のコードから隠したいかもしれません。

Access Levels for Frameworks フレームワークのためのアクセス水準

When you develop a framework, mark the public-facing interface to that framework as open or public so that it can be viewed and accessed by other modules, such as an app that imports the framework. This public-facing interface is the application programming interface (or API) for the framework. あなたがフレームワークを開発する場合、そのフレームワークを開放または公開するために公開向けインタフェースとして印してください、それで、他のモジュール、例えばそのフレームワークをインポートするアプリによってそれが見られてアクセスされることができます。この公開向けインタフェースは、フレームワークのためのアプリケーション・プログラミング・インタフェース(またはAPI)です。

Note 注意

Any internal implementation details of your framework can still use the default access level of internal, or can be marked as private or file private if you want to hide them from other parts of the framework’s internal code. You need to mark an entity as open or public only if you want it to become part of your framework’s API. あなたのフレームワークのあらゆる内部的実装詳細は、依然として省略時のアクセス水準である内部を使用します、もしくはあなたがそれらをそのフレームワークの内部コードの他の部分から隠したいならば非公開またはファイル限定として印されることができます。あなたは、それにあなたのフレームワークのもつAPIの一部になってほしいならば、ある実在を開放または公開として印する必要があります。

Access Levels for Unit Test Targets ユニットテストターゲットのためのアクセス水準

When you write an app with a unit test target, the code in your app needs to be made available to that module in order to be tested. By default, only entities marked as open or public are accessible to other modules. However, a unit test target can access any internal entity, if you mark the import declaration for a product module with the @testable attribute and compile that product module with testing enabled. あなたがユニットテストターゲットを使ってアプリを書く場合、あなたのアプリのコードはテストされるためにそのモジュールに対して利用可能にされる必要があります。特に何もしない状態では、開放または公開と印される実在だけが他のモジュールに利用可能です。しかしながら、あなたが製品モジュールのためのインポート宣言を@testable属性で印して、その製品モジュールをテスト可能な状態にコンパイルするならば、ユニットテストターゲットはあらゆる内部実在にアクセス可能です。

Access Control Syntax アクセス制御構文

Define the access level for an entity by placing one of the open, public, internal, fileprivate, or private modifiers at the beginning of the entity’s declaration. ある実在にアクセス水準を定義するには、openpublicinternalfileprivate、またはprivate修飾子のうちの1つをその実在の宣言の始まりに置いてください。

  1. public class SomePublicClass {}
  2. internal class SomeInternalClass {}
  3. fileprivate class SomeFilePrivateClass {}
  4. private class SomePrivateClass {}
  5. public var somePublicVariable = 0
  6. internal let someInternalConstant = 0
  7. fileprivate func someFilePrivateFunction() {}
  8. private func somePrivateFunction() {}

Unless otherwise specified, the default access level is internal, as described in Default Access Levels. This means that SomeInternalClass and someInternalConstant can be written without an explicit access-level modifier, and will still have an access level of internal: 特に明記しない限り、省略時のアクセス水準で記述されるように、省略時のアクセス水準は内部です。これは、SomeInternalClasssomeInternalConstantが明示的なアクセス水準修飾子なしで書かれることができ、それでもまだ内部のアクセス水準を持つことを意味します:

  1. class SomeInternalClass {} // implicitly internal(暗黙的に内部です)
  2. let someInternalConstant = 0 // implicitly internal(暗黙的に内部です)

Custom Types あつらえの型

If you want to specify an explicit access level for a custom type, do so at the point that you define the type. The new type can then be used wherever its access level permits. For example, if you define a file-private class, that class can only be used as the type of a property, or as a function parameter or return type, in the source file in which the file-private class is defined. あなたがあつらえの型のために明示的にアクセス水準を指定したいならば、あなたが型を定義する時点でそうしてください。その新しい型はそれから、そのアクセス水準が許すところどこででも使われることができます。例えば、あなたがあるファイル外非公開のクラスを定義するならば、そのクラスは、プロパティの型として、または関数パラメータや戻り型として、そのファイル外非公開クラスが定義されるソースファイルの中で使われることだけができます。

The access control level of a type also affects the default access level of that type’s members (its properties, methods, initializers, and subscripts). If you define a type’s access level as private or file private, the default access level of its members will also be private or file private. If you define a type’s access level as internal or public (or use the default access level of internal without specifying an access level explicitly), the default access level of the type’s members will be internal. ある型のアクセス制御水準はまた、その型のメンバー(それのプロパティ、メソッド、イニシャライザ、そして添え字)の省略時のアクセス水準に影響を与えます。あなたがある型のアクセス水準を非公開またはファイル外非公開として定義するならば、そのメンバーの省略時のアクセス水準も、非公開またはファイル外非公開になります。あなたがある型のアクセス水準を内部または公開として定義する(または明示的にアクセス水準を指定することなく省略時のアクセス水準である内部を使用する)ならば、その型のメンバーの省略時のアクセス水準は内部になります。

Important 重要

A public type defaults to having internal members, not public members. If you want a type member to be public, you must explicitly mark it as such. This requirement ensures that the public-facing API for a type is something you opt in to publishing, and avoids presenting the internal workings of a type as public API by mistake. 公開の型は特に何もしない状態では内部のメンバーを持ちます、公開のメンバーではなく。あなたがある型メンバーに公開であって欲しいならば、あなたは明確にそれをそのように印しなければなりません。この要件は、ある型のための公開向けAPIがあなたが公開するように選ぶ何かであることを確実にして、ある型の内部の働きを誤って公開のAPIとして公表することを避けます。

  1. public class SomePublicClass { // explicitly public class(明示的に公開のクラス)
  2. public var somePublicProperty = 0 // explicitly public class member(明示的に公開のクラス・メンバー)
  3. var someInternalProperty = 0 // implicitly internal class member(暗黙的に内部のクラス・メンバー)
  4. fileprivate func someFilePrivateMethod() {} // explicitly file-private class member(明示的にファイル外非公開のクラス・メンバー)
  5. private func somePrivateMethod() {} // explicitly private class member(明示的に非公開のクラス・メンバー)
  6. }
  7. class SomeInternalClass { // implicitly internal class(暗黙的に内部のクラス)
  8. var someInternalProperty = 0 // implicitly internal class member(暗黙的に内部のクラス・メンバー)
  9. fileprivate func someFilePrivateMethod() {} // explicitly file-private class member(明示的にファイル外非公開のクラス・メンバー)
  10. private func somePrivateMethod() {} // explicitly private class member(明示的に非公開のクラス・メンバー)
  11. }
  12. fileprivate class SomeFilePrivateClass { // explicitly file-private class(明示的にファイル外非公開のクラス)
  13. func someFilePrivateMethod() {} // implicitly file-private class member(暗黙的にファイル外非公開のクラス・メンバー)
  14. private func somePrivateMethod() {} // explicitly private class member(明示的に非公開のクラス・メンバー)
  15. }
  16. private class SomePrivateClass { // explicitly private class(明示的に非公開のクラス)
  17. func somePrivateMethod() {} // implicitly private class member(暗黙のうちに非公開のクラス・メンバー)
  18. }

Tuple Types タプル型

The access level for a tuple type is the most restrictive access level of all types used in that tuple. For example, if you compose a tuple from two different types, one with internal access and one with private access, the access level for that compound tuple type will be private. タプル型のためのアクセス水準は、そのタプルで使われる全ての型での最も制限的なアクセス水準です。例えば、あなたが2つの異なる型、内部アクセスを持つものと非公開アクセスを持つものからタプルを組み立てるならば、その複合のタプル型のためのアクセス水準は非公開になります。

Note 注意

Tuple types don’t have a standalone definition in the way that classes, structures, enumerations, and functions do. A tuple type’s access level is determined automatically from the types that make up the tuple type, and can’t be specified explicitly. タプル型には、クラス、構造体、列挙、そして関数が行う方法での、独立した定義がありません。あるタプル型のアクセス水準は、そのタプル型を作り上げる型から自動的に決定されます、したがって明示的に指定されることはできません。

Function Types 関数型

The access level for a function type is calculated as the most restrictive access level of the function’s parameter types and return type. You must specify the access level explicitly as part of the function’s definition if the function’s calculated access level doesn’t match the contextual default. 関数型のためのアクセス水準は、その関数のパラメータ型と戻り型での最も制限の多いアクセス水準と同じに算出されます。その関数の計算されたアクセス水準が省略時の文脈に合致しないと思うならば、あなたは関数の定義の一部として明確にアクセス水準を指定しなければなりません。

The example below defines a global function called someFunction(), without providing a specific access-level modifier for the function itself. You might expect this function to have the default access level of “internal”, but this isn’t the case. In fact, someFunction() won’t compile as written below: 下の例は、その関数自身に特定のアクセス水準修飾子を提供することなく、someFunction()と呼ばれるグローバル関数を定義します。あなたはこの関数が省略時アクセス水準の「内部」を持つのを期待するかもしれません、しかし事実はそうではありません。実際、someFunction()は以下のように書かれるときコンパイルしないでしょう:

  1. func someFunction() -> (SomeInternalClass, SomePrivateClass) {
  2. // function implementation goes here(関数実装が、ここにきます)
  3. }

The function’s return type is a tuple type composed from two of the custom classes defined above in Custom Types. One of these classes is defined as internal, and the other is defined as private. Therefore, the overall access level of the compound tuple type is private (the minimum access level of the tuple’s constituent types). この関数の戻り型は、上のあつらえの型で定義されるあつらえのクラスのうちの2つから作られるタプル型です。それらのクラスのうちの1つは内部として定義されます、そして別のものは非公開として定義されます。したがって、この複合タプル型の全体としてのアクセス水準は非公開です(このタプルの構成要素の型の内の最小のアクセス水準)。

Because the function’s return type is private, you must mark the function’s overall access level with the private modifier for the function declaration to be valid: 関数の戻り型が非公開であるので、あなたはこの関数宣言が有効になるために関数の全体的なアクセス水準にprivate修飾子で印しなければなりません:

  1. private func someFunction() -> (SomeInternalClass, SomePrivateClass) {
  2. // function implementation goes here(関数実装が、ここにきます)
  3. }

It’s not valid to mark the definition of someFunction() with the public or internal modifiers, or to use the default setting of internal, because public or internal users of the function might not have appropriate access to the private class used in the function’s return type. someFunction()の定義がpublicまたはinternalを使って印されること、または省略時設定の内部を使うことは有効ではありません、なぜなら、この関数の公開や内部のユーザーは適切なアクセスを関数の戻り型の中で使われる非公開クラスに対して持たないからです。

Enumeration Types 列挙型

The individual cases of an enumeration automatically receive the same access level as the enumeration they belong to. You can’t specify a different access level for individual enumeration cases. ある列挙の個々のケース節は、自動的にそれらが属している列挙と同じアクセス水準を与えられます。あなたは、個々の列挙ケース節に対して異なるアクセス水準を指定することができません。

In the example below, the CompassPoint enumeration has an explicit access level of public. The enumeration cases north, south, east, and west therefore also have an access level of public: 以下の例において、CompassPoint列挙は明示的な公開のアクセス水準を持ちます。この列挙のケース節northsoutheast、そしてwestはしたがってまた公開のアクセス水準を持ちます:

  1. public enum CompassPoint {
  2. case north
  3. case south
  4. case east
  5. case west
  6. }

Raw Values and Associated Values 生の値と関連値

The types used for any raw values or associated values in an enumeration definition must have an access level at least as high as the enumeration’s access level. For example, you can’t use a private type as the raw-value type of an enumeration with an internal access level. 列挙定義の中のあらゆる生の値または関連値のために使われる型は、少なくとも列挙のアクセス水準と同じの高さのアクセス水準を持たなければなりません。例えば、あなたはある非公開型を、内部のアクセス水準をもつ列挙の「生の値」型として使うことができません。

Nested Types 入れ子にされた型

The access level of a nested type is the same as its containing type, unless the containing type is public. Nested types defined within a public type have an automatic access level of internal. If you want a nested type within a public type to be publicly available, you must explicitly declare the nested type as public. 入れ子にされた型のアクセス水準は、それの容れ物である型と同じです、容れ物である型が公開である場合を除いて。公開型の内側で定義される入れ子にされた型は、自動的なアクセス水準として内部を持ちます。あなたが公開型の内側で入れ子にされた型に公開的に利用できて欲しいならば、あなたは明確に入れ子にされた型を公開として宣言しなければなりません。

Subclassing サブクラスをつくる

You can subclass any class that can be accessed in the current access context and that’s defined in the same module as the subclass. You can also subclass any open class that’s defined in a different module. A subclass can’t have a higher access level than its superclass—for example, you can’t write a public subclass of an internal superclass. あなたは、現在のアクセス前後関係においてアクセスされることが出来るどんなクラスにでもサブクラスを作れます、それは同じモジュールの中でサブクラスとして定義されます。あなたはまた、異なるモジュールにおいて定義されるどんな開放クラスにでもサブクラスを作れます。サブクラスには、そのスーパークラスより高いアクセス水準があることができません ― 例えば、あなたは内部のスーパークラスに属する公開のサブクラスを書くことができません。

In addition, for classes that are defined in the same module, you can override any class member (method, property, initializer, or subscript) that’s visible in a certain access context. For classes that are defined in another module, you can override any open class member. それに加えて、同じモジュールにおいて定義されるクラスに対して、あなたは特定のアクセス前後関係において可視であるあらゆるクラスメンバー(メソッド、プロパティ、イニシャライザ、または添え字)をオーバーライドすることができます。別のモジュールにおいて定義されるクラスに対して、あなたはどんな開放クラスメンバーでもオーバーライドできます。

An override can make an inherited class member more accessible than its superclass version. In the example below, class A is a public class with a file-private method called someMethod(). Class B is a subclass of A, with a reduced access level of “internal”. Nonetheless, class B provides an override of someMethod() with an access level of “internal”, which is higher than the original implementation of someMethod(): オーバーライドは、継承されたクラス・メンバーを、そのスーパークラス版よりもアクセス度を高くすることができます。下記の例で、クラスAは公開のクラスです、それはsomeMethod()と呼ばれるあるファイル外非公開のメソッドを持ちます。クラスBAのサブクラスです、そして減少させられたアクセス水準の「内部」を持ちます。それにもかかわらず、クラスBは、「内部」のアクセス水準を持つsomeMethod()のオーバーライドを提供します、それは元々のsomeMethod()の実装よりもより高いです:

  1. public class A {
  2. fileprivate func someMethod() {}
  3. }
  4. internal class B: A {
  5. override internal func someMethod() {}
  6. }

It’s even valid for a subclass member to call a superclass member that has lower access permissions than the subclass member, as long as the call to the superclass’s member takes place within an allowed access level context (that is, within the same source file as the superclass for a file-private member call, or within the same module as the superclass for an internal member call): スーパークラスのメンバーへの呼び出しがある許されるアクセス水準文脈内で(すなわち、ファイル外非公開メンバー呼び出しのためのスーパークラスと同じソースファイル内で、または内部メンバー呼び出しのためのスーパークラスと同じモジュール内で)起こる限り、サブクラスメンバーより低いアクセス許可を持つスーパークラスメンバーを呼び出すことは、サブクラスメンバーにとってさえ有効です:

  1. public class A {
  2. fileprivate func someMethod() {}
  3. }
  4. internal class B: A {
  5. override internal func someMethod() {
  6. super.someMethod()
  7. }
  8. }

Because superclass A and subclass B are defined in the same source file, it’s valid for the B implementation of someMethod() to call super.someMethod(). スーパークラスAとサブクラスBが同じソースファイルにおいて定義されるので、B実装のsomeMethod()super.someMethod()を呼ぶことは有効です。

Constants, Variables, Properties, and Subscripts 定数、変数、プロパティ,そして添え字

A constant, variable, or property can’t be more public than its type. It’s not valid to write a public property with a private type, for example. Similarly, a subscript can’t be more public than either its index type or return type. 定数、変数、またはプロパティは、その型よりもより公開であることはできません。例えば、非公開の型で公開のプロパティを書くことは、有効ではありません。同じように、添え字はそのインデックス型や戻り型よりも公開であることはできません。

If a constant, variable, property, or subscript makes use of a private type, the constant, variable, property, or subscript must also be marked as private: 定数、変数、プロパティ、または添え字が非公開の型を利用するならば、定数、変数、プロパティ、または添え字はまた、privateと印しされなければなりません:

  1. private var privateInstance = SomePrivateClass()

Getters and Setters ゲッターとセッター

Getters and setters for constants, variables, properties, and subscripts automatically receive the same access level as the constant, variable, property, or subscript they belong to. 定数、変数、プロパティ、そして添え字のためのゲッターとセッターは、自動的に、それらが属している定数、変数、プロパティまたは添え字と同じアクセス水準を受けます。

You can give a setter a lower access level than its corresponding getter, to restrict the read-write scope of that variable, property, or subscript. You assign a lower access level by writing fileprivate(set), private(set), or internal(set) before the var or subscript introducer. あなたは、セッターにその対応するゲッターよりもより低いアクセス水準を与えて、その変数、プロパティ、または添え字の読み書きのスコープを制限することができます。あなたは、より低いアクセス水準を割り当てることを、fileprivate(set)private(set)、またはinternal(set)varまたはsubscript導入子の前に書くことによって行います。

Note 注意

This rule applies to stored properties as well as computed properties. Even though you don’t write an explicit getter and setter for a stored property, Swift still synthesizes an implicit getter and setter for you to provide access to the stored property’s backing storage. Use fileprivate(set), private(set), and internal(set) to change the access level of this synthesized setter in exactly the same way as for an explicit setter in a computed property. この規則は、格納プロパティに、それだけでなく計算プロパティにも適用されます。たとえあなたが格納プロパティのために明確なゲッターとセッターを書かないとしても、スウィフトは格納プロパティのもつ支援外部記憶へのアクセスを提供するためにあなたのためにまだ暗黙のゲッターとセッターを総合的に扱います。fileprivate(set)private(set)、そしてinternal(set)を使って、計算プロパティにおける明示的なセッターと正確に同じ方法で、この総合されたセッターのアクセス水準を変更してください。

The example below defines a structure called TrackedString, which keeps track of the number of times a string property is modified: 下の例はTrackedStringと呼ばれる構造体を定義します、それは、ある文字列プロパティが修正される回数を追跡します:

  1. struct TrackedString {
  2. private(set) var numberOfEdits = 0
  3. var value: String = "" {
  4. didSet {
  5. numberOfEdits += 1
  6. }
  7. }
  8. }

The TrackedString structure defines a stored string property called value, with an initial value of "" (an empty string). The structure also defines a stored integer property called numberOfEdits, which is used to track the number of times that value is modified. This modification tracking is implemented with a didSet property observer on the value property, which increments numberOfEdits every time the value property is set to a new value. TrackedString構造体は、valueと呼ばれる格納文字列プロパティを、""(空の文字列)の初期値で定義します。この構造体はまた、numberOfEditsと呼ばれる格納整数プロパティを定義します、それは、valueが修正される回数を追跡するために使われます。この修正追跡は、didSetプロパティオブザーバーとしてvalueプロパティ上で実行されます、それは、numberOfEditsvalueプロパティが新しい値に設定されるたびに増加させます。

The TrackedString structure and the value property don’t provide an explicit access-level modifier, and so they both receive the default access level of internal. However, the access level for the numberOfEdits property is marked with a private(set) modifier to indicate that the property’s getter still has the default access level of internal, but the property is settable only from within code that’s part of the TrackedString structure. This enables TrackedString to modify the numberOfEdits property internally, but to present the property as a read-only property when it’s used outside the structure’s definition. TrackedString構造体とvalueプロパティは明確なアクセス水準修飾子を提供しません、なので、それらは両方とも省略時のアクセス水準である内部を受け取ります。しかし、numberOfEditsプロパティのためのアクセス水準は、private(set)修飾子を使って印を付けられて、そのプロパティの持つゲッターが依然として省略時のアクセス水準の内部を持つ、しかしプロパティはTrackedString構造体の部分であるコード内からのみ設定可能であることを指し示します。これがTrackedStringに可能にするのは、numberOfEditsプロパティを内部のように修正すること、しかしそのプロパティをそれが構造体の定義の外側で使われる時は読み出し専用プロパティとして提供することです。

If you create a TrackedString instance and modify its string value a few times, you can see the numberOfEdits property value update to match the number of modifications: あなたがTrackedStringインスタンスをつくって、2、3回その文字列値を修正するならば、あなたはnumberOfEditsプロパティが修正の数に合うように更新されるのを見ることができます:

  1. var stringToEdit = TrackedString()
  2. stringToEdit.value = "This string will be tracked."
  3. stringToEdit.value += " This edit will increment numberOfEdits."
  4. stringToEdit.value += " So will this one."
  5. print("The number of edits is \(stringToEdit.numberOfEdits)")
  6. // Prints "The number of edits is 3"(「編集数は、3です」を出力します)

Although you can query the current value of the numberOfEdits property from within another source file, you can’t modify the property from another source file. This restriction protects the implementation details of the TrackedString edit-tracking functionality, while still providing convenient access to an aspect of that functionality. あなたは別のソースファイル内からnumberOfEditsプロパティの現在の値についてたずねることができるけれども、あなたは別のソースファイルからこのプロパティを修正できません。この制限は、TrackedString編集追跡機能性の実装詳細を保護します、その一方でその機能性の一面への便利なアクセスを依然として提供します。

Note that you can assign an explicit access level for both a getter and a setter if required. The example below shows a version of the TrackedString structure in which the structure is defined with an explicit access level of public. The structure’s members (including the numberOfEdits property) therefore have an internal access level by default. You can make the structure’s numberOfEdits property getter public, and its property setter private, by combining the public and private(set) access-level modifiers: あなたは明示的なアクセス水準をゲッターとセッターの両方に割り当てることが、必要ならば可能です。以下の例は、TrackedString構造体のある改作を示します、そこにおいてこの構造体は公開の明示的なアクセス水準を使って定義されます。構造体のメンバー(numberOfEditsプロパティを含む)は、したがって特に何もしなければ内部のアクセス水準を持ちます。あなたは、この構造体のnumberOfEditsプロパティゲッターを公開に、そしてそれのプロパティセッターを非公開にすることが、publicprivate(set)アクセス水準修飾子を組み合わせることによって可能です。

  1. public struct TrackedString {
  2. public private(set) var numberOfEdits = 0
  3. public var value: String = "" {
  4. didSet {
  5. numberOfEdits += 1
  6. }
  7. }
  8. public init() {}
  9. }

Initializers イニシャライザ

Custom initializers can be assigned an access level less than or equal to the type that they initialize. The only exception is for required initializers (as defined in Required Initializers). A required initializer must have the same access level as the class it belongs to. あつらえのイニシャライザは、それらが初期化する型と同じか低いアクセス水準を割り当てられることができます。唯一の例外は、必須イニシャライザに対してです(必須イニシャライザで定義されるように)。必須イニシャライザは、それが属しているクラスと同じアクセス水準を持たなければなりません。

As with function and method parameters, the types of an initializer’s parameters can’t be more private than the initializer’s own access level. 関数やメソッドパラメータと同様に、イニシャライザのパラメーターの型は、そのイニシャライザ自身のアクセス水準よりも非公開にすることはできません。

Default Initializers 省略時のイニシャライザ

As described in Default Initializers, Swift automatically provides a default initializer without any arguments for any structure or base class that provides default values for all of its properties and doesn’t provide at least one initializer itself. 省略時のイニシャライザで記述されるように、スウィフトは、全く引数を持たない省略時のイニシャライザを、あらゆる構造体または基盤クラスで、それのプロパティの全てに省略時の値を提供して、とにかく1つのイニシャライザもそれ自身では提供しないもののために、自動的に提供します。

A default initializer has the same access level as the type it initializes, unless that type is defined as public. For a type that’s defined as public, the default initializer is considered internal. If you want a public type to be initializable with a no-argument initializer when used in another module, you must explicitly provide a public no-argument initializer yourself as part of the type’s definition. 省略時のイニシャライザは、それが初期化する型と同じアクセス水準を持ちます、その型がpublicとして宣言される場合を除いて。publicとして定義される型のために、その省略時のイニシャライザは内部と見なされます。あなたがある公開型を別のモジュール内で使うときに引数のないイニシャライザを使って初期化できるようにしたいならば、あなたはある公開の引数のないイニシャライザをあなた自身でその型定義の一部として明示的に提供しなければなりません。

Default Memberwise Initializers for Structure Types 構造体体型のための省略時のメンバー関連イニシャライザ

The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal. 構造体型のための省略時のメンバー関連イニシャライザは、その構造体の格納プロパティのいずれかが非公開ならば、非公開であるとみなされます。同様に、構造体の持つ格納プロパティのいずれかがファイル外非公開ならば、イニシャライザはファイル外非公開です。そうでなければ、イニシャライザは内部のアクセス水準を持ちます。

As with the default initializer above, if you want a public structure type to be initializable with a memberwise initializer when used in another module, you must provide a public memberwise initializer yourself as part of the type’s definition. 上記の省略時のイニシャライザと同様に、あなたがある公開の構造体型を別のモジュール内で使うときにメンバー関連イニシャライザを使って初期化できるようにしたいならば、あなたは公開のメンバー関連イニシャライザをあなた自身でその型定義の一部として提供しなければなりません。

Protocols プロトコル

If you want to assign an explicit access level to a protocol type, do so at the point that you define the protocol. This enables you to create protocols that can only be adopted within a certain access context. あなたが明確なアクセス水準をあるプロトコル型に割り当てたいならば、あなたがそのプロトコルを定義する時点でそうしてください。これは、あなたに特定のアクセス文脈内で採用されることだけができるプロトコルを作成するのを可能にします。

The access level of each requirement within a protocol definition is automatically set to the same access level as the protocol. You can’t set a protocol requirement to a different access level than the protocol it supports. This ensures that all of the protocol’s requirements will be visible on any type that adopts the protocol. プロトコル定義内の各要件のアクセス水準は、自動的にプロトコルと同じアクセス水準に設定されます。あなたは、あるプロトコル要件をそれが支えるプロトコルと異なるアクセス水準に設定することができません。これは、プロトコルを採用するあらゆる型上で、プロトコルのもつ要件の全てが見えることを確実にします。

Note 注意

If you define a public protocol, the protocol’s requirements require a public access level for those requirements when they’re implemented. This behavior is different from other types, where a public type definition implies an access level of internal for the type’s members. あなたがある公開プロトコルを定義するならば、そのプロトコルの持つ要件は、それら要件に対してそれらが実装されるときに公開アクセス水準を必要とします。この挙動は他の型とは異なります、そこにおいて、公開型定義は、その型のメンバーに対して内部アクセス水準を暗黙的に意味します。

Protocol Inheritance プロトコル継承

If you define a new protocol that inherits from an existing protocol, the new protocol can have at most the same access level as the protocol it inherits from. For example, you can’t write a public protocol that inherits from an internal protocol. あなたが既存のプロトコルから継承する新しいプロトコルを定義するならば、新しいプロトコルは最大ではそれが継承するプロトコルと同じアクセス水準を持つことができます。例えば、あなたは内部のプロトコルから継承する公開のプロトコルを書くことができません。

Protocol Conformance プロトコル準拠

A type can conform to a protocol with a lower access level than the type itself. For example, you can define a public type that can be used in other modules, but whose conformance to an internal protocol can only be used within the internal protocol’s defining module. ある型は、その型自体よりも低いアクセス水準を持つプロトコルに準拠することができます。たとえば、あなたは、別のモジュール内で使われることができる公開の型を定義することができます、しかし内部プロトコルに準拠するものは、ただその内部プロトコルの定義するモジュール内で使われることができるだけです。

The context in which a type conforms to a particular protocol is the minimum of the type’s access level and the protocol’s access level. For example, if a type is public, but a protocol it conforms to is internal, the type’s conformance to that protocol is also internal. ある型が特定のプロトコルに準拠するところの前後関係は、その型のアクセス水準とプロトコルのアクセス水準の最も低いものです。例えば、ある型が公開である、しかしそれが準拠するプロトコルは内部であるならば、その型のもつそのプロトコルに対する準拠もまた内部です。

When you write or extend a type to conform to a protocol, you must ensure that the type’s implementation of each protocol requirement has at least the same access level as the type’s conformance to that protocol. For example, if a public type conforms to an internal protocol, the type’s implementation of each protocol requirement must be at least internal. あなたがある型をプロトコルに準拠するように書く、または拡張する時は、あなたはその型のもつ各プロトコル要件の実装が少なくともそのプロトコルに対するその型の準拠と同じアクセス水準を持つことを確かにしてください。例えば、ある公開型が内部プロトコルに準拠するならば、その型の持つ各プロトコル要件の実装は、少なくとも内部でなければなりません。

Note 注意

In Swift, as in Objective-C, protocol conformance is global—it isn’t possible for a type to conform to a protocol in two different ways within the same program. スウィフトにおいて、Objective-Cにおいてと同様に、プロトコル準拠はグローバルです ― ひとつの型にとって、同じプログラム内でひとつのプロトコルに2つの異なる方法で準拠することは、可能ではありません。

Extensions 拡張

You can extend a class, structure, or enumeration in any access context in which the class, structure, or enumeration is available. Any type members added in an extension have the same default access level as type members declared in the original type being extended. If you extend a public or internal type, any new type members you add have a default access level of internal. If you extend a file-private type, any new type members you add have a default access level of file private. If you extend a private type, any new type members you add have a default access level of private. あなたは、あるクラス、構造体、または列挙を拡張することが、そこにおいてそのクラス、構造体、または列挙が利用可能であるあらゆるアクセス文脈において可能です。拡張において加えられるどんな型メンバーでも、拡張されているところの本来の型で宣言される型メンバーと同じ省略時のアクセス水準を持ちます。あなたがある公開または内部型を拡張するならば、あなたが加えるどんな新しい型メンバーでも省略時のアクセス水準である内部を持ちます。あなたがあるファイル外非公開型を拡張するならば、あなたが加えるどんな新しい型メンバーでも省略時のアクセス水準であるファイル外非公開を持ちます。あなたがある非公開型を拡張するならば、あなたが加えるどんな新しい型メンバーでも省略時のアクセス水準である非公開を持ちます。

Alternatively, you can mark an extension with an explicit access-level modifier (for example, private) to set a new default access level for all members defined within the extension. This new default can still be overridden within the extension for individual type members. 代わりに、あなたは拡張に明確なアクセス水準修飾子(例えばprivate)で印して、新しい省略時のアクセス水準をその拡張内で定義される全てのメンバーに設定することができます。この新しい省略時設定はさらに、個々の型メンバーのために拡張内でオーバーライドされることができます。

You can’t provide an explicit access-level modifier for an extension if you’re using that extension to add protocol conformance. Instead, the protocol’s own access level is used to provide the default access level for each protocol requirement implementation within the extension. あなたがプロトコル準拠を加えるためにその拡張を使っているならば、あなたは明確なアクセス水準修飾子を拡張のために用意することが出来ません。その代わりに、プロトコル自身のアクセス水準が、省略時のアクセス水準を拡張内の各プロトコル要件実施のために提供するために使われます。

Private Members in Extensions 拡張の中の非公開メソッド

Extensions that are in the same file as the class, structure, or enumeration that they extend behave as if the code in the extension had been written as part of the original type’s declaration. As a result, you can: それが拡張するクラス、構造体、または列挙と同じファイルの中にある拡張は、まるでその拡張の中のコードがオリジナルの型の持つ宣言の一部として書かれたかのように振る舞います。結果として、あなたは次のことが出来ます:

  • Declare a private member in the original declaration, and access that member from extensions in the same file. ある非公開メンバーをオリジナルの宣言の中で宣言する、そしてそのメンバーに対して同じファイル中の拡張からアクセスする。
  • Declare a private member in one extension, and access that member from another extension in the same file. ある非公開メンバーを一方の拡張の中で宣言する、そしてそのメンバーに対して同じファイル中の別の拡張からアクセスする。
  • Declare a private member in an extension, and access that member from the original declaration in the same file. ある非公開メンバーをある拡張の中で宣言する、そしてそのメンバーに対して同じファイル中のオリジナルの宣言からアクセスする。

This behavior means you can use extensions in the same way to organize your code, whether or not your types have private entities. For example, given the following simple protocol: この挙動は、あなたの型が非公開な実在を持つかどうかにかかわらず、あなたが拡張を同じ方法で使ってあなたのコードを編成できるのを意味します。例えば、以下の簡単なプロトコルを与えられて:

  1. protocol SomeProtocol {
  2. func doSomething()
  3. }

You can use an extension to add protocol conformance, like this: あなたはある拡張を使って、このようにプロトコル準拠を加えることが出来ます:

  1. struct SomeStruct {
  2. private var privateVariable = 12
  3. }
  4. extension SomeStruct: SomeProtocol {
  5. func doSomething() {
  6. print(privateVariable)
  7. }
  8. }

Generics 総称体

The access level for a generic type or generic function is the minimum of the access level of the generic type or function itself and the access level of any type constraints on its type parameters. 総称体型または総称体関数のためのアクセス水準は、総称体型または関数自身のアクセス水準およびその型パラメータ上のあらゆる型制約のアクセス水準の最少のものです。

Type Aliases 型エイリアス

Any type aliases you define are treated as distinct types for the purposes of access control. A type alias can have an access level less than or equal to the access level of the type it aliases. For example, a private type alias can alias a private, file-private, internal, public, or open type, but a public type alias can’t alias an internal, file-private, or private type. あなたが定義するどんな型エイリアスでも、アクセス制御の目的のためにはっきりした型とみなされます。型エイリアスは、それがエイリアスする型のアクセス水準と同等またはそれより下のアクセス水準を持つことができます。例えば、非公開の型エイリアスは、非公開、ファイル外非公開、内部、公開、または開放型のエイリアス(別名)となることができます、しかし公開型エイリアスは、内部、ファイル外非公開、または非公開型をエイリアスできません。

Note 注意

This rule also applies to type aliases for associated types used to satisfy protocol conformances. この規則はまた、プロトコル準拠を満たすために使われる関連型のための型エイリアスにも適用されます。

Memory Safety メモリ安全

Advanced Operators 先進の演算子