Протокол - это "контракт" в Swift, который определяет набор требований (методов, свойств и других функциональностей), которым должен соответствовать любой тип, который его принимает.
protocol MyProtocol {
// требования протокола
}
Протоколы задают четкие интерфейсы для взаимодействия между компонентами:
protocol DataFetcher {
func fetchData(completion: (Result<Data, Error>) -> Void)
}
class NetworkManager: DataFetcher {
func fetchData(completion: (Result<Data, Error>) -> Void) {
// реализация сетевого запроса
}
}
Классический паттерн в iOS-разработке:
protocol UITableViewDelegate: AnyObject {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
}
class MyViewController: UIViewController, UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// обработка нажатия
}
}
Альтернатива ООП, особенно полезная в Swift:
protocol Drawable {
func draw()
}
extension Drawable {
func draw() { print("Default drawing") }
}
struct Circle: Drawable {
func draw() { print("Drawing circle") }
}
С помощью протоколов с ассоциированными типами:
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
Легко создавать mock-объекты для тестирования:
protocol DatabaseService {
func save(data: Data) -> Bool
}
class MockDatabase: DatabaseService {
func save(data: Data) -> Bool { return true }
}
protocol FullyNamed {
var fullName: String { get } // может быть let или var
static var defaultName: String { get set }
}
protocol RandomNumberGenerator {
func random() -> Double
static func systemRandom() -> Double
mutating func reset() // для структур
}
protocol Initializable {
init(parameters: [String: Any])
}
class MyClass: Initializable {
required init(parameters: [String: Any]) { ... }
}
Протоколы можно использовать как полноценные типы:
let fetchers: [DataFetcher] = [NetworkFetcher(), LocalFetcher()]
Protocol Composition:
func configure(controller: UIViewController & UITableViewDataSource)
Optional Requirements (только для @objc протоколов):
@objc protocol OptionalProtocol {
@objc optional func optionalMethod()
}
Protocol Extensions - мощный механизм для добавления реализации по умолчанию:
extension Collection {
var isNotEmpty: Bool { !isEmpty }
}
// Протокол для работы с API
protocol APIClient {
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T
}
// Реализация для production
class NetworkAPIClient: APIClient {
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T {
// реальный сетевой запрос
}
}
// Mock для тестов
class MockAPIClient: APIClient {
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T {
// возвращаем заранее подготовленные данные
}
}
Протоколы в Swift используются для:
Протоколы - это фундаментальная концепция Swift, делающая код более модульным, тестируемым и гибким.