Extensions
Protocol Conformance
An extension can make an existing type conform to a protocol.
Add conformance separately
protocol_conformance.swift
protocol StatusLabel {
func statusLabel() -> String
}
struct Build {
let passed: Bool
}
extension Build: StatusLabel {
func statusLabel() -> String {
return passed ? "passed" : "failed"
}
}
let passed =
let build = Build(passed: passed)
let label = build.statusLabel()
print(label)
protocol StatusLabel {
func statusLabel() -> String
}
struct Build {
let passed: Bool
}
extension Build: StatusLabel {
func statusLabel() -> String {
return passed ? "passed" : "failed"
}
}
let passed =
let build = Build(passed: passed)
let label = build.statusLabel()
print(label)
extension conformance
Protocol conformance can be declared in an extension when the required members fit better outside the original type declaration.