d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
67 lines
1.8 KiB
Markdown
67 lines
1.8 KiB
Markdown
---
|
|
paths:
|
|
- "**/*.swift"
|
|
- "**/Package.swift"
|
|
---
|
|
# Swift パターン
|
|
|
|
> このファイルは [common/patterns.md](../common/patterns.md) を Swift 固有のコンテンツで拡張します。
|
|
|
|
## プロトコル指向設計
|
|
|
|
小さく焦点を絞ったプロトコルを定義する。共有デフォルトにはプロトコル拡張を使用する:
|
|
|
|
```swift
|
|
protocol Repository: Sendable {
|
|
associatedtype Item: Identifiable & Sendable
|
|
func find(by id: Item.ID) async throws -> Item?
|
|
func save(_ item: Item) async throws
|
|
}
|
|
```
|
|
|
|
## 値型
|
|
|
|
- データ転送オブジェクトとモデルには構造体を使用する
|
|
- 異なる状態をモデリングするには関連値付きの列挙型を使用する:
|
|
|
|
```swift
|
|
enum LoadState<T: Sendable>: Sendable {
|
|
case idle
|
|
case loading
|
|
case loaded(T)
|
|
case failed(Error)
|
|
}
|
|
```
|
|
|
|
## アクターパターン
|
|
|
|
ロックやディスパッチキューの代わりに、共有ミュータブル状態にはアクターを使用する:
|
|
|
|
```swift
|
|
actor Cache<Key: Hashable & Sendable, Value: Sendable> {
|
|
private var storage: [Key: Value] = [:]
|
|
|
|
func get(_ key: Key) -> Value? { storage[key] }
|
|
func set(_ key: Key, value: Value) { storage[key] = value }
|
|
}
|
|
```
|
|
|
|
## 依存性注入
|
|
|
|
デフォルトパラメータ付きでプロトコルを注入する — 本番ではデフォルトを使用し、テストではモックを注入する:
|
|
|
|
```swift
|
|
struct UserService {
|
|
private let repository: any UserRepository
|
|
|
|
init(repository: any UserRepository = DefaultUserRepository()) {
|
|
self.repository = repository
|
|
}
|
|
}
|
|
```
|
|
|
|
## 参考
|
|
|
|
アクターベースの永続化パターンについてはスキル: `swift-actor-persistence` を参照。
|
|
プロトコルベースの DI とテストについてはスキル: `swift-protocol-di-testing` を参照。
|