プロトコルとエクステンション【Swiftイントロダクション#6】
プロトコル
プロトコルを定義するにはprotocolを使います。
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
struct SimpleStructure: ExampleProtocol {
var simpleDescription: String = "A simple structure"
//構造体を変更するためmutatingをつける
mutating func adjust() {
simpleDescription += " (adjusted)"
}
}
var b = SimpleStructure()
b.adjust()
let bDescription = b.simpleDescription
SimpleStructureの宣言にmutatingが使われていることに注目してください。このadjust()メソッドが構造体を変更するため、mutatingが必要になります。SimpleClassのメソッドにはmutatingは不要です。これは、クラスのメソッドは常にクラスに変更を加えることが可能だからです。
エクステンション
extensionを使って、型にメソッドなどの新しい機能を足すことができます。また、extensionを使うと、他のファイルで定義された型やライブラリやフレームワークからインポートしてきた型をプロトコルに準拠させることもできます。
//Int型をExampleProtocolに準拠させる
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
print(7.simpleDescription)
プロトコル名は他の名前付きの型(クラス、列挙型、構造体)と同じように使うことができます。例えば、一つのプロトコルに準拠する違う型を持つオブジェクトをコレクションとして持つことができます。プロトコル型の値を扱うとき、プロトコル定義外のメソッドは利用できません。
let protocolValue: ExampleProtocol = a
print(protocolValue.simpleDescription)
// print(protocolValue.anotherProperty) // Uncomment to see the error
変数protocolValueはaが代入されているのでランタイムではSimpleClassの型を持ちますが、コンパイラーはExampleProtocolの型を持っているとみなします。
なので、ExampleProtocolで宣言されていないanotherPropertyにアクセスしようとするとコンパイルエラーになります。
これによってクラスがプロトコル準拠以外で実装するメソッドやプロパティに偶然アクセスするようなことがなくなります!
次回は【エラー処理】について解説します!
今までのSwiftイントロダクション
#1 シンプルな値
#2 制御フロー
#3 関数とクロージャ
#4 オブジェクトとクラス
#5 列挙型と構造体
この記事が気に入ったらサポートをしてみませんか?