[Swift] Protocols and Extensions

SwiftにもObjective-Cみたいにprotocolとextensionがあります。
ProtocolはJavaで言う所のInterfaceのようなものです。
tableviewのデータソースやデリゲートなどで使います。

プロトコル

プロトコルは異なるTypeにも適応できるので、複数のタイプを同じプロトコルとして扱う事が可能です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// protocolキーワードでプロトコルを定義する
// プロトコルを継承したクラスはプロトコルを実装していないとコンパイル時にエラーになる
protocol ExampleProtocol {
    var simpleDescription: String { get }
    // struct, enumで自身の値を変更する場合は
    // mutatingキーワードが必要
    // classの場合は不要
    mutating func adjust()
}
 
// Class, enumeration, structは全てプロトコルを継承できる
class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class."
    var anotherProperty: Int = 1123
    func adjust() {
        simpleDescription += " Now 100% adjusted."
    }
}
 
var a = SimpleClass()
a.adjust()
print(a.simpleDescription)
 
struct SimpleStrucure: ExampleProtocol {
    var simpleDescription: String = "A simple structure."
    mutating func adjust() {
        simpleDescription += " (adjusted)"
    }
}
 
var b = SimpleStrucure()
b.adjust()
print(b.simpleDescription)

Extension

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 既存の定数型にもプロトコルを適応して拡張することができる
extension Int: ExampleProtocol {
    var simpleDescription: String {
        return "The number \(self)"
    }
    mutating func adjust() {
        self += 7
    }
}
 
print(7.simpleDescription)
var hoge = 7;
hoge.adjust()
print(hoge)
 
// プロトコルを用いることで複数の違うタイプを
// 同じ方法で扱うことが可能になる
let protocolValue: ExampleProtocol = a
print(protocolValue.simpleDescription)

参考URL

0 件のコメント :

コメントを投稿