Skip to content

Latest commit

 

History

History
93 lines (73 loc) · 3.17 KB

swift-intro-constants.md

File metadata and controls

93 lines (73 loc) · 3.17 KB

home seiten a-z intro <-- hoch --> runter home/swift/intro/constants

Konstanten

Konstanten sind Objekte zur Speicherung von Werten und/oder Funktionen, die nicht geändert werden können. Für die Deklarierung wird das Schlüsselwort 'let' genutzt.

// Allgemein
let konstante: Typ = wert    // Die Konstante 'konstante' vom Typ 'Typ' soll den Wert 'wert' haben.
// Beispiel 1
let c1: String = "volker"  
let c2 = "volker"            // Compiler bestimmt den Typ [type inference]
// Beispiel 2a
// Die Konstante 'function' speichert eine Funktion 'gruesse', die als Funktion übergeben wird.
func gruesse () -> String {
    return "hallo"
}
let function: () -> String = gruesse                             // print(function()) --> hallo
// Beispiel 2b
// Die Konstante 'function' speichert eine Funktion 'gruesse', die als Funktion übergeben wird.
func gruesse () -> String {
    return "hallo"
}
let function: () -> String = gruesse                             // print(function()) --> hallo

// Der Compiler bestimmt den Typ [type inference]
let function = gruesse                                           // print(function()) --> hallo
// Beispiel 3a
// Die Konstante 'closure' speichert eine Funktion, die als Closure übergeben wird.
let closure: () -> String = { () -> String in return "hallo" }   // print(closure()) --> hallo
// Beispiel 3b
// Der Compiler bestimmt den Typ [type inference]
let closure: () -> String = { () -> String in return "hallo" }   // print(closure()) --> hallo

// Ohne Typangabe in Closure
let closure: () -> String = { return "hallo" }                   // print(closure()) --> hallo

// Ohne 'return' in Closure
let closure: () -> String = { "hallo" }                          // print(closure()) --> hallo

// Ohne Typangabe für Konstante
let closure = { "hallo" }                                        // print(closure()) --> hallo

// Ohne Closure
let closure = "hallo"                                            // print(closure()) --> hallo

Links:

buch

Videos:


home seiten a-z <-- hoch --> rauf