Swift - Protocols: Unterschied zwischen den Versionen
| (2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 2: | Zeile 2: | ||
[[Swift (Programmiersprache)]] | [[Swift (Programmiersprache)]] | ||
https://docs.swift.org/swift-book/LanguageGuide/Protocols.html | https://docs.swift.org/swift-book/LanguageGuide/Protocols.html | ||
| + | [[Swift - Delegate]] | ||
== Einführung == | == Einführung == | ||
| − | Protokolle garantieren, dass Klassen oder Structs bestimmte Funktionen haben. Wird der Klasse oder dem Struct ein Protokoll zugewiesen, muss sie die Funktionen des Protokolls implementieren. Wie sie das tut bleibt der Klasse überlassen. | + | Protokolle garantieren, dass Klassen oder Structs bestimmte '''Funktionen''' haben. Wird der Klasse oder dem Struct ein Protokoll zugewiesen, muss sie die Funktionen des Protokolls implementieren. Wie sie das tut bleibt der Klasse überlassen. |
| − | Protokolle kann man als Datentyp verwenden. So kann man bei Funktionsaufrufen z.B. mehrere Klassen erlauben. Wichtig ist dann nur, dass sie die Vorgaben des Protokolls erfüllen. Das Protokoll ist also eine Art Garantie, dass eine Klasse bestimmte | + | Protokolle kann man '''als Datentyp''' verwenden. So kann man bei Funktionsaufrufen z.B. mehrere Klassen erlauben. Wichtig ist dann nur, dass sie die Vorgaben des Protokolls erfüllen. Das Protokoll ist also eine Art '''Garantie, dass eine Klasse bestimmte Funktionalitäten erfüllt'''. |
Man sagt auch eine Klasse adaptiert ein (oder mehrere) Protokoll(e) | Man sagt auch eine Klasse adaptiert ein (oder mehrere) Protokoll(e) | ||
| Zeile 13: | Zeile 14: | ||
Wie Datentypen oder Klassennamen beginnen Protokollnamen mit einem Großbuchstaben und werden in CamelCase notiert. | Wie Datentypen oder Klassennamen beginnen Protokollnamen mit einem Großbuchstaben und werden in CamelCase notiert. | ||
| + | |||
| + | In UIKit werden Protocols oft in Zusammenhang mit dem '''Delegate Pattern''' verwendet (siehe Links). | ||
== Beispiele == | == Beispiele == | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
| Zeile 99: | Zeile 102: | ||
The eagle flaps it's wings and lifts off into the sky | The eagle flaps it's wings and lifts off into the sky | ||
</pre> | </pre> | ||
| + | |||
| + | == Typealias == | ||
| + | Über einen Typealias kann man mehrere Protokolle statt mit Komma getrennt mit einem Schlüsselwort verwenden. So steht z.B. der Typealias '''Codable''' für die Kombination '''Decodable, Encodable''' | ||
Aktuelle Version vom 19. Januar 2023, 11:34 Uhr
Links[Bearbeiten]
Swift (Programmiersprache) https://docs.swift.org/swift-book/LanguageGuide/Protocols.html Swift - Delegate
Einführung[Bearbeiten]
Protokolle garantieren, dass Klassen oder Structs bestimmte Funktionen haben. Wird der Klasse oder dem Struct ein Protokoll zugewiesen, muss sie die Funktionen des Protokolls implementieren. Wie sie das tut bleibt der Klasse überlassen.
Protokolle kann man als Datentyp verwenden. So kann man bei Funktionsaufrufen z.B. mehrere Klassen erlauben. Wichtig ist dann nur, dass sie die Vorgaben des Protokolls erfüllen. Das Protokoll ist also eine Art Garantie, dass eine Klasse bestimmte Funktionalitäten erfüllt.
Man sagt auch eine Klasse adaptiert ein (oder mehrere) Protokoll(e)
class MyClass: Superclass, FirstProtocol, AnotherProtocol, ...
Wie Datentypen oder Klassennamen beginnen Protokollnamen mit einem Großbuchstaben und werden in CamelCase notiert.
In UIKit werden Protocols oft in Zusammenhang mit dem Delegate Pattern verwendet (siehe Links).
Beispiele[Bearbeiten]
//
// main.swift
// Protocol Demo
//
// Created by Stephan Schlegel on 07.01.23.
//
/**
Klassen oder Structs mit dem protocol CanFly MÜSSEN die Funktion fly() IMPLEMENTIEREN
Es wird nicht vorgegeben wie diese im Detail aussehen soll. So kann ein Flugzeug
anders fliegen wie ein Adler
Das Protokoll ist wie ein Zertifikat das GARANTIERT, dass eine Klasse bestimmte FÄHIGKEITEN hat. Auf der anderen Seite kann man das Protokoll einsetzen, wenn man bestimmte Eigenschaften nicht in der Klasse definieren möchte. Z.B. möchten wir keine fly() Funktion in der Klasse Birds implementieren, da z.B. Pinguine gar nicht fliegen können.
*/
protocol CanFly {
func fly()
}
class Bird {
var isFemale = true
func layEgg(){
if(isFemale){
print("The bird makes a new bird in a shell.")
}
}
}
// penguins don't fly
class Penguin: Bird {
func swim(){
print("The penguin paddles through the water")
}
}
// eagles do
class Eagle: Bird, CanFly { // ADOPTS CanFly
func soar(){
print("The eagle glides in the air using air currents")
}
func fly(){
print("The eagle flaps it's wings and lifts off into the sky")
}
}
struct Airplane: CanFly { // Structs can use protocols too
func fly() { // Airplanes do fly but don't flap wings
print("The airplane uses it's engine to lift off into the sky")
}
}
struct FlyingMuseum{
/**
We can use the protocol as a DATATYPE. This let's us use different Objects but guarantees that all of them have a fly function
*/
func flyingShow(flyingObject: CanFly){
print("Watch our show - ")
flyingObject.fly()
}
}
// Let's go
let myEagle = Eagle()
let myPenguin = Penguin()
let myPlane = Airplane()
myEagle.fly()
myPlane.fly()
myPenguin.layEgg() // inherited from Bird
let museum = FlyingMuseum()
museum.flyingShow(flyingObject: myPlane) // sure to work because of protocol CanFly
museum.flyingShow(flyingObject: myEagle)
Ausgabe:
The eagle flaps it's wings and lifts off into the sky The airplane uses it's engine to lift off into the sky The bird makes a new bird in a shell. Watch our show - The airplane uses it's engine to lift off into the sky Watch our show - The eagle flaps it's wings and lifts off into the sky
Typealias[Bearbeiten]
Über einen Typealias kann man mehrere Protokolle statt mit Komma getrennt mit einem Schlüsselwort verwenden. So steht z.B. der Typealias Codable für die Kombination Decodable, Encodable