Protocols
Method Requirements
Protocols can require conforming types to implement methods.
Require a method
method_requirements.swift
protocol Summarizable {
func summary() -> String
}
struct Ticket: Summarizable {
let id: Int
let owner: String
func summary() -> String {
return "#\(id) \(owner)"
}
}
let ticketId =
let ticket = Ticket(id: ticketId, owner: "Ada")
let summary = ticket.summary()
print(summary)
protocol Summarizable {
func summary() -> String
}
struct Ticket: Summarizable {
let id: Int
let owner: String
func summary() -> String {
return "#\(id) \(owner)"
}
}
let ticketId =
let ticket = Ticket(id: ticketId, owner: "Ada")
let summary = ticket.summary()
print(summary)
protocol Summarizable {
func summary() -> String
}
struct Ticket: Summarizable {
let id: Int
let owner: String
func summary() -> String {
return "#\(id) \(owner)"
}
}
let ticketId =
let ticket = Ticket(id: ticketId, owner: "Ada")
let summary = ticket.summary()
print(summary)
method requirement
A method requirement describes the method signature that each conforming type must implement.