MENU

【Swift入門】protocolとは?初心者でもわかるプロトコルの基礎と使い方

Swiftでアプリ開発を学んでいると、「protocol」という言葉に出会うことがあります。特にSwiftUIを学ぶ際には頻繁に登場します。この記事では、Swiftのprotocolについて、プログラミング初心者の方にもわかりやすく解説します。

目次

Swiftのprotocolとは?

protocolは「プロトコル」または「規約」と呼ばれ、特定の機能を実現するために必要なメソッドやプロパティの設計図を定義するものです。

わかりやすく例えると、protocolは「約束事」や「契約書」のようなものです。「この機能を持ちたいなら、これらのメソッドを実装してください」という要件を定義します。

// 「挨拶できる」という機能の設計図
protocol Greetable {
    var name: String { get }
    func greet()
}

なぜprotocolを使うのか?

protocolを使わない場合、同じような機能を持つ型を作るときに、それぞれ独立して実装することになります。

// protocolを使わない場合
struct Dog {
    func makeSound() {
        print("ワンワン")
    }
}

struct Cat {
    func makeSound() {
        print("ニャー")
    }
}

// 共通の型として扱えない
let dog = Dog()
let cat = Cat()
// これらを配列にまとめることができない

protocolを使えば、異なる型を共通のインターフェースで扱えます。

// protocolを使う場合
protocol Animal {
    func makeSound()
}

struct Dog: Animal {
    func makeSound() {
        print("ワンワン")
    }
}

struct Cat: Animal {
    func makeSound() {
        print("ニャー")
    }
}

// 共通の型として扱える
let animals: [Animal] = [Dog(), Cat()]
for animal in animals {
    animal.makeSound()
}

protocolの基本的な書き方

protocolは以下のような構文で定義します。

protocol プロトコル名 {
    // プロパティの要求
    var プロパティ名: 型 { get }  // 読み取り専用
    var プロパティ名: 型 { get set }  // 読み書き可能
    
    // メソッドの要求
    func メソッド名() -> 戻り値の型
}

実際の例を見てみましょう。

protocol Identifiable {
    var id: String { get }
    var name: String { get set }
    func displayInfo()
}

// protocolに準拠した構造体
struct User: Identifiable {
    var id: String
    var name: String
    
    func displayInfo() {
        print("ID: \(id), 名前: \(name)")
    }
}

let user = User(id: "001", name: "太郎")
user.displayInfo()  // ID: 001, 名前: 太郎

protocolの主な機能

1. プロパティの要求

protocolでは、準拠する型が持つべきプロパティを定義できます。

protocol Vehicle {
    var speed: Double { get set }
    var maxSpeed: Double { get }  // 読み取り専用
}

struct Car: Vehicle {
    var speed: Double
    var maxSpeed: Double
    
    init() {
        self.speed = 0.0
        self.maxSpeed = 180.0
    }
}

var car = Car()
car.speed = 60.0
print(car.speed)  // 60.0

2. メソッドの要求

protocolでは、準拠する型が実装すべきメソッドを定義できます。

protocol Drawable {
    func draw()
    func resize(width: Double, height: Double)
}

struct Circle: Drawable {
    var radius: Double
    
    func draw() {
        print("半径 \(radius) の円を描画")
    }
    
    func resize(width: Double, height: Double) {
        print("円のサイズを変更")
    }
}

let circle = Circle(radius: 5.0)
circle.draw()  // 半径 5.0 の円を描画

3. 複数のprotocolに準拠

Swiftでは、1つの型が複数のprotocolに準拠できます。

protocol Named {
    var name: String { get }
}

protocol Aged {
    var age: Int { get }
}

protocol Addressable {
    var address: String { get }
}

// 複数のprotocolに準拠
struct Person: Named, Aged, Addressable {
    var name: String
    var age: Int
    var address: String
}

let person = Person(name: "太郎", age: 25, address: "東京都")

4. protocolの継承

protocolは他のprotocolを継承できます。

protocol Named {
    var name: String { get }
}

protocol Identifiable: Named {
    var id: String { get }
}

// Identifiableに準拠すると、自動的にNamedにも準拠する
struct Product: Identifiable {
    var id: String
    var name: String  // Namedプロトコルの要求
}

5. デフォルト実装(Extension)

extensionを使って、protocolにデフォルトの実装を提供できます。

protocol Greetable {
    var name: String { get }
    func greet()
}

// デフォルト実装を提供
extension Greetable {
    func greet() {
        print("こんにちは、\(name)です!")
    }
}

struct Student: Greetable {
    var name: String
    // greetメソッドを実装しなくてもOK
}

let student = Student(name: "花子")
student.greet()  // こんにちは、花子です!

実践的なprotocolの使用例

例1:データ保存機能の抽象化

protocol Saveable {
    func save()
    func load()
}

struct Document: Saveable {
    var title: String
    var content: String
    
    func save() {
        print("\(title) を保存しました")
    }
    
    func load() {
        print("\(title) を読み込みました")
    }
}

struct Image: Saveable {
    var filename: String
    
    func save() {
        print("画像 \(filename) を保存しました")
    }
    
    func load() {
        print("画像 \(filename) を読み込みました")
    }
}

// 異なる型を同じprotocol型として扱える
let items: [Saveable] = [
    Document(title: "レポート", content: "内容"),
    Image(filename: "photo.jpg")
]

for item in items {
    item.save()
}
// 出力:
// レポート を保存しました
// 画像 photo.jpg を保存しました

例2:ゲームキャラクターの共通機能

protocol GameCharacter {
    var name: String { get }
    var health: Int { get set }
    var attackPower: Int { get }
    
    func attack() -> Int
    func takeDamage(_ damage: Int)
}

extension GameCharacter {
    func attack() -> Int {
        return attackPower
    }
    
    func takeDamage(_ damage: Int) {
        health -= damage
        if health < 0 {
            health = 0
        }
        print("\(name) が \(damage) のダメージを受けた(残りHP: \(health))")
    }
}

struct Warrior: GameCharacter {
    var name: String
    var health: Int
    var attackPower: Int
    
    // デフォルト実装を使用
}

struct Mage: GameCharacter {
    var name: String
    var health: Int
    var attackPower: Int
    
    // attackメソッドをカスタマイズ
    func attack() -> Int {
        return attackPower * 2  // 魔法使いは2倍のダメージ
    }
}

var warrior = Warrior(name: "戦士", health: 100, attackPower: 20)
var mage = Mage(name: "魔法使い", health: 80, attackPower: 15)

warrior.takeDamage(30)  // 戦士 が 30 のダメージを受けた(残りHP: 70)
print("魔法使いの攻撃力: \(mage.attack())")  // 魔法使いの攻撃力: 30

例3:API通信の統一インターフェース

protocol APIRequestable {
    var endpoint: String { get }
    func fetch(completion: @escaping (String) -> Void)
}

extension APIRequestable {
    func fetch(completion: @escaping (String) -> Void) {
        print("\(endpoint) からデータを取得中...")
        // 実際の通信処理をここに実装
        completion("取得したデータ")
    }
}

struct UserRequest: APIRequestable {
    var endpoint: String = "/api/users"
}

struct ProductRequest: APIRequestable {
    var endpoint: String = "/api/products"
}

let userRequest = UserRequest()
userRequest.fetch { data in
    print("ユーザーデータ: \(data)")
}
// 出力:
// /api/users からデータを取得中...
// ユーザーデータ: 取得したデータ

protocolとclass/structの使い分け

protocolは「何ができるか」を定義し、class/structは「何であるか」を定義します。

特徴protocolstruct/class
定義するもの機能の要件(インターフェース)データと実装
実装実装を持たない(extensionで可能)実装を持つ
準拠複数のprotocolに準拠可能classは単一継承のみ
使用場面共通の機能を強制したい具体的なデータ構造を作りたい

組み合わせて使う例

// protocolで「できること」を定義
protocol Flyable {
    func fly()
}

protocol Swimmable {
    func swim()
}

// structで「何であるか」を定義し、protocolで「できること」を追加
struct Duck: Flyable, Swimmable {
    var name: String
    
    func fly() {
        print("\(name) が飛んでいます")
    }
    
    func swim() {
        print("\(name) が泳いでいます")
    }
}

let duck = Duck(name: "アヒル")
duck.fly()   // アヒル が飛んでいます
duck.swim()  // アヒル が泳いでいます

protocolを使うメリット

  1. 柔軟な設計が可能:複数のprotocolに準拠でき、機能を組み合わせられます
  2. コードの再利用性が向上:共通の機能をprotocolで定義し、複数の型で使い回せます
  3. テストがしやすい:protocolを使うことでモック(偽物)を作りやすくなります
  4. 疎結合な設計:具体的な型に依存せず、protocolに依存することで変更に強くなります
  5. SwiftUIとの親和性:SwiftUIはprotocolベースで設計されています

SwiftUIでのprotocol

SwiftUIを学ぶ際、protocolは必須の知識です。実際にViewもprotocolです。

import SwiftUI

// ViewはSwiftUIのprotocol
struct ContentView: View {
    // bodyプロパティの実装が必須
    var body: some View {
        Text("Hello, World!")
    }
}

よくある間違いと注意点

間違い1:プロパティの要求を満たしていない

protocol Named {
    var name: String { get set }
}

// エラー:nameが定数(let)なので書き込みできない
struct Person: Named {
    let name: String  // コンパイルエラー
}

// 正しい実装
struct Person: Named {
    var name: String  // OK
}

間違い2:必須メソッドの実装忘れ

protocol Drawable {
    func draw()
}

// エラー:drawメソッドを実装していない
struct Circle: Drawable {
    // drawメソッドがない!
}  // コンパイルエラー

間違い3:mutatingキーワードの不足

structでプロパティを変更するメソッドにはmutatingが必要です。

protocol Counter {
    var count: Int { get set }
    mutating func increment()
}

struct SimpleCounter: Counter {
    var count: Int
    
    mutating func increment() {
        count += 1
    }
}

まとめ

Swiftのprotocolは、柔軟で保守しやすいコードを書くための強力な機能です。以下のポイントを覚えておきましょう。

  • protocolは機能の設計図や契約を定義する
  • 異なる型に共通のインターフェースを持たせられる
  • 複数のprotocolに準拠できる(多重継承のような柔軟性)
  • extensionでデフォルト実装を提供できる
  • SwiftUIなど、Swiftの多くのフレームワークで活用されている
  • テストしやすく、変更に強いコード設計が可能

protocolをマスターすることで、より高度なSwiftプログラミングができるようになります。特にSwiftUIを学ぶ際には必須の知識なので、しっかり理解しておきましょう。

プログラミングの独学におすすめ
プログラミング言語の人気オンラインコース
独学でプログラミングを学習している方で、エラーなどが発生して効率よく勉強ができないと悩む方は多いはず。Udemyは、プロの講師が動画で実際のプログラムを動かしながら教えてくれるオンライン講座です。講座の価格は、セール期間中には専門書籍を1冊買うよりも安く済むことが多いです。新しく学びたいプログラミング言語がある方は、ぜひUdemyでオンライン講座を探してみてください。
目次