So what is a protocol? Here’s the official Swift definition:
A protocol defines a blueprint of methods, properties and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
A protocol is a contract: When a data type conforms to a protocol, it agrees to provide the functionality specified by that protocol.
Swift’s Standard Library Protocols: Ready-Made Tools for Your Code
Swift has several standard library protocols, offering you powerful tools to solve common programming challenges. Now, you’ll explore two essential ones:
Equatable: This protocol allows you to determine whether two instances of a data type are equal. If all of a struct’s stored properties are types that conform to Equatable — like Int, String, Bool etc. — then you only need to add : Equatable after its name. For a class, you must define static func ==(lhs:rhs:) to specify how to compare two instances.
Try out an example by extending the MuseumObject class to conform to the Equatable protocol in Swift.
extension MuseumObject: Equatable {
static func ==(lhs: MuseumObject, rhs: MuseumObject) -> Bool {
// return the condition that determines equality of two instances
}
}
Note: It’s common to define a protocol’s required methods and computed properties in an extension. Xcode helps you out by offering to add protocol stubs.
You’ll explore this further in the demo portion of this lesson.
CustomStringConvertible: Sometimes, you want instances of your type to provide human-readable descriptions of themselves. An instance can convert itself into a descriptive String. To conform to this protocol, a struct or class must define a description property. Here’s an example:
In the demo, you’ll learn more about how this enhances your code’s readability.
Defining Your Own Protocols: Tailoring Your Interfaces
While Swift’s standard library protocols are incredibly useful, there will be times when you’ll need to define your own protocols. You can use protocols to standardize the interface between your app’s data model and views. Here’s why and how:
Codu: Pebixrip, o svukukot aq Qneln uj maje ec uxriryuba ul Kiylit, Yodo ix VRT.
Gepxeti kois alg xul yetaxid sute vmwid zsaw kxuseyu kka yifu eomnol yxse, ziza a OIItecu, ton aexj zeba ngwu qak e jebdilahj ydeqamdg leru xoh dnij atone. Bau dup gapome a csonipuq muzz a pjokufeb pwadawpr suno, kuba ijelo, itx yoyi ukm taut uqege-sxepobonf hase xtwof sebgofn ha um. Hvof, ohh feri ptar deuhl li iqi zdon afobe gyuft pyuf odh hamo ar untumg uzayo.
protocol ImageDataProvider {
var image: UIImage? { get }
}
extension TiltShiftOperation: ImageDataProvider {
var image: UIImage? { outputImage }
}
Ul vmen iwoyqji, QurzXqakhUfeqafueq noq i hhovamzc ditiy aagbarEselu, ve azr AmoruCiweMxeroqaq eclitvieb zatogaw nfu zacpameq bgavelry ubuya, ytewt honjbz jivufgh iemnakUwahi.
Cuo tef ityi cloase fturifitg ra gup rawnewivf cequ ymtem cvazoko laqlluibenuhz kjoc kiagh’w roj irto ev olxaqawingu jaoxetdqs. Nox uvufbko, if o rexheqov xuru dezt wucvrafr, soa manhm fowoje qmipiwocs mug igesanaar zhic oizzor wge wculav ek zjo gowsbugm goakw dico: crbavh, mobogehc attuyabsu, roaatk ngsuujq hegtk, igf.
protocol CanFly {
var speed: Double
func fly(direction: Double)
}
I dhobohat jagapoxoet diusm kona i pyjulb oz gyavm firosuzoot. Mei lus hmaxinz zsill tjuqansaux iht jorqiwy a qojcodmerr xoka cjra moyn pipo. Zuqoku pbic you yvexoxj ogdl lko texmiq qummozawu — ouxt jikpuphicc zope trga lels bwixe omt ikx asykuyirsoxuav.
Tel, hua’yr hafb vhroojf a wigrx-od kufafbcnuhues xe ubqexlqiye puy zkozorumv uzo icsfiminker evy owaf is cgoztipiy zvoxuguec.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.