欠如

来世は貝になりたい

ツアーですねProtocols and Extensions

ツアーですね 今回はProtocols and Extensions プロトコルと拡張がテーマとなります。

まずはじめにプロトコルとは何かですが Wikiでは以下の通りに載ってます

プロトコルまたはプロトコール(英語: protocol 英語発音: [ˈproutəˌkɔːl] プロウタコール、[ˈproutəˌkɔl] プロウタコル、フランス語: protocole フランス語発音: [prɔtɔkɔl] プロトコル)とは、 複数の者が対象となる事項を確実に実行するための手順等について定めたもの。

Use protocol to declare a protocol. プロトコルを使用してプロトコルを宣言します。

protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust() }

Classes, enumerations, and structs can all adopt protocols. クラス、列挙体、および構造体は、すべてプロトコルを採用できます。

class SimpleClass: ExampleProtocol { var simpleDescription: String = “A very simple class.” var anotherProperty: Int = 69105 func adjust() { simpleDescription += “ Now 100% adjusted.” } } var a = SimpleClass() a.adjust() let aDescription = a.simpleDescription

struct SimpleStructure: ExampleProtocol { var simpleDescription: String = “A simple structure” mutating func adjust() { simpleDescription += “ (adjusted)” } } var b = SimpleStructure() b.adjust() let bDescription = b.simpleDescription

Notice the use of the mutating keyword in the declaration of SimpleStructure to mark a method that modifies the structure. The declaration of SimpleClass doesn’t need any of its methods marked as mutating because methods on a class can always modify the class.

SimpleStructureの宣言でmutatingキーワードを使用して構造を変更するメソッドをマークすることに注目してください。 SimpleClassの宣言では、クラスのメソッドは常にクラスを変更できるため、そのメソッドの中には突然変異としてマークされているものは必要ありません。

Use extension to add functionality to an existing type, such as new methods and computed properties. You can use an extension to add protocol conformance to a type that is declared elsewhere, or even to a type that you imported from a library or framework.

拡張機能を使用して、新しいメソッドや計算されたプロパティなど、既存のタイプに機能を追加します。 拡張機能を使用して、他の場所で宣言されている型、またはライブラリやフレームワークからインポートした型にプロトコル準拠を追加することができます。

extension Int: ExampleProtocol { var simpleDescription: String { return “The number (self)” } mutating func adjust() { self += 42 } } print(7.simpleDescription)

上記を実行するとThe number (self)となりselfに7が入る形になります

You can use a protocol name just like any other named type—for example, to create a collection of objects that have different types but that all conform to a single protocol. When you work with values whose type is a protocol type, methods outside the protocol definition are not available.

プロトコル名は、他の名前付きタイプと同じように使用できます。たとえば、異なるタイプのオブジェクトのコレクションを作成しますが、すべてが単一のプロトコルに準拠しています。 タイプがプロトコルタイプである値を操作する場合、プロトコル定義外のメソッドは使用できません。

protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust() } class SimpleClass: ExampleProtocol { var simpleDescription: String = “A very simple class.” var anotherProperty: Int = 69105 func adjust() { simpleDescription += “ Now 100% adjusted.” } } var a = SimpleClass() a.adjust() let aDescription = a.simpleDescription

let protocolValue: ExampleProtocol = a print(protocolValue.simpleDescription) // print(protocolValue.anotherProperty) // Uncomment to see the error (コメントを外すとエラー)

表示結果としてはA very simple class. Now 100% adjusted. となります。

Even though the variable protocolValue has a runtime type of SimpleClass, the compiler treats it as the given type of ExampleProtocol. This means that you can’t accidentally access methods or properties that the class implements in addition to its protocol conformance.

変数protocolValueに実行時の型SimpleClassがある場合でも、コンパイラはそれを指定された型のExampleProtocolとして扱います。 つまり、プロトコルの適合性に加えて、クラスが実装するメソッドやプロパティに誤ってアクセスすることはできません。