Fehlermeldungen verstehen
Eine Swift-Fehlermeldung besteht aus einer error-Zeile mit Datei, Zeile und Spalte, gefolgt von note-Zeilen mit Hinweisen. Die note enthält oft schon die Lösung. Hier findest du die Meldungen, die beim Lernen am häufigsten auftauchen, jeweils mit dem Code, der sie auslöst, der echten Ausgabe des Compilers, der Ursache und der Lösung.
Danach kannst du
- liest den Aufbau einer Swift-Fehlermeldung
- findest zu einer Meldung Ursache und Lösung
- unterscheidest Compilerfehler, Warnungen und Laufzeitabbrüche
Voraussetzungen
Keine. Du kannst hier einsteigen.
Umgebung
Swift 6.4.0, Sprachmodus 6
Meldungen erzeugt unter Linux x86-64; Xcode zeigt dieselben Meldungen in seiner eigenen Darstellung
Der Wortlaut kann sich zwischen Swift-Versionen leicht ändern. Jede Meldung trägt das Datum, an dem sie erzeugt wurde.
So liest du eine Meldung
Eine typische Meldung sieht so aus:
nicht-entpackt.swift:2:12: error: value of optional type 'String?' must be unwrapped …
1 | let eingabe: String? = "Swift"
2 | let laut = eingabe.uppercased()
| |- error: value of optional type 'String?' must be unwrapped …
| |- note: chain the optional using '?' …
| `- note: force-unwrap using '!' …
nicht-entpackt.swift:2:12sind Datei, Zeile und Spalte. Die Zeilennummern am Rand zeigen den Code darum herum, die Markierung zeigt genau auf die Stelle.errorheißt: Das Programm wird nicht übersetzt.warningheißt: Es wird übersetzt, aber etwas ist wahrscheinlich nicht so gemeint.notesind Hinweise. Sehr oft steht in der erstennoteschon die Lösung.- Manche Meldungen enden mit einem Verweis wie
[#SendingClosureRisksDataRace]und einem Link auf eine ausführliche Erklärung auf swift.org.
Lies immer zuerst die erste Meldung. Ein einziger Fehler kann Folgefehler auslösen, die verschwinden, sobald der erste behoben ist.
Laufzeitabbrüche sehen anders aus. Das Programm wurde übersetzt und gestartet und stoppt dann mit Fatal error oder Swift runtime failure. Sie entstehen durch Programmierfehler wie einen ungültigen Index oder ein ! auf nil.
Meldung nachschlagen
Füge unten einen Teil deiner Meldung ein oder tippe ein Stichwort. Die Suche läuft nur in deinem Browser, nichts wird gesendet. Alle Meldungen stehen darunter auch als vollständige Liste.
Alle Meldungen
-
Optional verwendet, ohne es zu entpacken
Compilerfehler
error: value of optional type 'String?' must be unwrapped to refer to member 'uppercased' of wrapped base type 'String'Ursache: Der Wert hat einen Optional-Typ wie String?, und du rufst eine Methode oder Eigenschaft auf, die es nur für den eigentlichen Typ gibt.
Lösung: Entpacke den Wert vorher mit if let oder guard let, nutze Optional Chaining mit ?. oder einen Ersatzwert mit ??.
Mehr dazu in der Lektion Optionals.
Code und vollständige Ausgabe
nicht-entpackt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let eingabe: String? = "Swift" let laut = eingabe.uppercased()Compiler-Meldung
nicht-entpackt.swift:2:12: error: value of optional type 'String?' must be unwrapped to refer to member 'uppercased' of wrapped base type 'String' 1 | let eingabe: String? = "Swift" 2 | let laut = eingabe.uppercased() | |- error: value of optional type 'String?' must be unwrapped to refer to member 'uppercased' of wrapped base type 'String' | |- note: chain the optional using '?' to access member 'uppercased' only for non-'nil' base values | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 3 |
-
Mit einem Optional gerechnet
Compilerfehler
error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'Ursache: Ein Dictionary-Zugriff oder eine Umwandlung wie Int("…") liefert ein Optional. Mit Optionals kannst du nicht direkt rechnen.
Lösung: Gib mit ?? einen Ersatzwert an, zum Beispiel (lager["Äpfel"] ?? 0) + 10, oder entpacke mit if let.
Mehr dazu in der Lektion Collections.
Code und vollständige Ausgabe
dictionary-optional.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let lager = ["Äpfel": 12] let nachLieferung = lager["Äpfel"] + 10Compiler-Meldung
dictionary-optional.swift:2:26: error: value of optional type 'Int?' must be unwrapped to a value of type 'Int' 1 | let lager = ["Äpfel": 12] 2 | let nachLieferung = lager["Äpfel"] + 10 | |- error: value of optional type 'Int?' must be unwrapped to a value of type 'Int' | |- note: coalesce using '??' to provide a default when the optional value contains 'nil' | `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil' 3 |
-
if let mit einem Wert, der kein Optional ist
Compilerfehler
error: initializer for conditional binding must have Optional type, not 'String'Ursache: if let und guard let entpacken nur Optionals. Der Wert hier hat einen normalen Typ und kann nie nil sein.
Lösung: Lass if let weg und verwende den Wert direkt.
Mehr dazu in der Lektion Optionals.
Code und vollständige Ausgabe
kein-optional.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let name = "Ada" if let name { print(name) }Compiler-Meldung
kein-optional.swift:2:4: error: initializer for conditional binding must have Optional type, not 'String' 1 | let name = "Ada" 2 | if let name { | `- error: initializer for conditional binding must have Optional type, not 'String' 3 | print(name) 4 | } -
Absturz: Ausrufezeichen hat nil getroffen
Laufzeitabbruch
error: Unexpectedly found nil while unwrapping an Optional valueUrsache: Ein Force Unwrap mit ! wurde auf ein Optional ohne Wert angewendet. Swift beendet das Programm an dieser Stelle.
Lösung: Ersetze ! durch if let, guard let oder ??, besonders bei allem, was von außen kommt.
Mehr dazu in der Lektion Optionals.
Code und vollständige Ausgabe
erzwungen.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let alter = Int("zweiundvierzig") print(alter! + 1)Abbruch zur Laufzeit
erzwungen.swift:2: Fatal error: Unexpectedly found nil while unwrapping an Optional value
-
Konstante nachträglich geändert
Compilerfehler
error: cannot assign to value: 'kapitel' is a 'let' constantUrsache: Ein mit let angelegter Wert wird später neu zugewiesen.
Lösung: Prüfe, ob die Änderung nötig ist. Wenn ja, lege den Wert mit var an.
Mehr dazu in der Lektion Variablen und Typen.
Code und vollständige Ausgabe
let-aendern.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let kapitel = 1 kapitel = 2Compiler-Meldung
let-aendern.swift:2:1: error: cannot assign to value: 'kapitel' is a 'let' constant 1 | let kapitel = 1 | `- note: change 'let' to 'var' to make it mutable 2 | kapitel = 2 | `- error: cannot assign to value: 'kapitel' is a 'let' constant 3 |
-
Int und Double gemischt
Compilerfehler
error: binary operator '*' cannot be applied to operands of type 'Int' and 'Double'Ursache: Swift wandelt Zahlentypen nie automatisch um. Für eine Mischung aus Int und Double gibt es keinen passenden Operator.
Lösung: Wandle einen Wert ausdrücklich um, zum Beispiel Double(stueck) * preis.
Mehr dazu in der Lektion Variablen und Typen.
Code und vollständige Ausgabe
typen-mischen.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let stueck = 3 let preis = 2.5 let summe = stueck * preisCompiler-Meldung
typen-mischen.swift:3:20: error: binary operator '*' cannot be applied to operands of type 'Int' and 'Double' 1 | let stueck = 3 2 | let preis = 2.5 3 | let summe = stueck * preis | |- error: binary operator '*' cannot be applied to operands of type 'Int' and 'Double' | `- note: overloads for '*' exist with these partially matching parameter lists: (Double, Double), (Duration, Double), (Int, Int) 4 |
-
Text einer Zahl zugewiesen
Compilerfehler
error: cannot convert value of type 'String' to specified type 'Int'Ursache: Ein String wird einer Konstante vom Typ Int zugewiesen. Swift wandelt Text nicht stillschweigend in Zahlen um.
Lösung: Nutze Int(text). Das liefert ein Int?, weil nicht jeder Text eine Zahl ist.
Mehr dazu in der Lektion Variablen und Typen.
Code und vollständige Ausgabe
string-zu-int.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let eingabe = "42" let zahl: Int = eingabeCompiler-Meldung
string-zu-int.swift:2:17: error: cannot convert value of type 'String' to specified type 'Int' 1 | let eingabe = "42" 2 | let zahl: Int = eingabe | `- error: cannot convert value of type 'String' to specified type 'Int' 3 |
-
Absturz: arithmetischer Überlauf
Laufzeitabbruch
Swift runtime failure: arithmetic overflowUrsache: Eine Rechnung hat den Wertebereich des Zahlentyps überschritten, hier Int8 über 127.
Lösung: Nimm einen größeren Typ wie Int, prüfe vorher die Grenzen, oder nutze bewusst die Überlaufoperatoren wie &+.
Mehr dazu in der Lektion Variablen und Typen.
Code und vollständige Ausgabe
ueberlauf.swift Swift 6.4.0, Linux x86-64, 19.10.2026 var punkte: Int8 = 120 for _ in 1...10 { punkte += 1 } print(punkte)Abbruch zur Laufzeit
*** Swift runtime failure: arithmetic overflow ***
-
switch deckt nicht alle Fälle ab
Compilerfehler
error: switch must be exhaustiveUrsache: Ein switch muss jeden möglichen Wert behandeln. Bei einem Enum fehlt mindestens ein Fall, die note nennt ihn.
Lösung: Ergänze den fehlenden Fall. Bei offenen Wertebereichen wie Int oder String ergänze default.
Mehr dazu in der Lektion Kontrollfluss.
Code und vollständige Ausgabe
switch-unvollstaendig.swift Swift 6.4.0, Linux x86-64, 19.10.2026 enum Ampel { case rot, gelb, gruen } func darfFahren(_ ampel: Ampel) -> Bool { switch ampel { case .gruen: return true case .rot: return false } }Compiler-Meldung
switch-unvollstaendig.swift:6:5: error: switch must be exhaustive 4 | 5 | func darfFahren(_ ampel: Ampel) -> Bool { 6 | switch ampel { | |- error: switch must be exhaustive | `- note: add missing case: '.gelb' 7 | case .gruen: 8 | return true -
Absturz: Array-Index außerhalb des Bereichs
Laufzeitabbruch
error: Index out of rangeUrsache: Der Index ist größer als der letzte gültige Index oder negativ. Ein Array mit drei Elementen hat die Indizes 0 bis 2.
Lösung: Prüfe mit indices.contains(i), nutze first und last, oder laufe mit for über die Elemente statt über Indizes.
Mehr dazu in der Lektion Collections.
Code und vollständige Ausgabe
index-ausserhalb.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let tage = ["Mo", "Di", "Mi"] print(tage[3])Abbruch zur Laufzeit
Swift/ContiguousArrayBuffer.swift:695: Fatal error: Index out of range
-
Argument-Label fehlt
Compilerfehler
error: missing argument label 'name:' in callUrsache: Die Funktion erwartet beim Aufruf ein Label vor dem Argument.
Lösung: Schreibe das Label dazu, oder setze in der Definition _ vor den Parameter, wenn kein Label gewünscht ist.
Mehr dazu in der Lektion Funktionen und Closures.
Code und vollständige Ausgabe
label-fehlt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 func begruesse(name: String) -> String { "Hallo, \(name)!" } print(begruesse("Ada"))Compiler-Meldung
label-fehlt.swift:5:17: error: missing argument label 'name:' in call 3 | } 4 | 5 | print(begruesse("Ada")) | `- error: missing argument label 'name:' in call 6 | -
Überflüssiges Argument-Label
Compilerfehler
error: extraneous argument label 'x:' in callUrsache: Beim Aufruf steht ein Label, das die Funktion nicht hat, zum Beispiel weil der Parameter mit _ definiert ist.
Lösung: Lass das Label beim Aufruf weg.
Mehr dazu in der Lektion Funktionen und Closures.
Code und vollständige Ausgabe
label-zuviel.swift Swift 6.4.0, Linux x86-64, 19.10.2026 func quadrat(_ x: Int) -> Int { x * x } let q = quadrat(x: 4)Compiler-Meldung
label-zuviel.swift:2:16: error: extraneous argument label 'x:' in call 1 | func quadrat(_ x: Int) -> Int { x * x } 2 | let q = quadrat(x: 4) | `- error: extraneous argument label 'x:' in call 3 | -
Closure hat die falsche Zahl von Parametern
Compilerfehler
error: contextual closure type '(Int, Int) throws -> Bool' expects 2 arguments, but 1 was used in closure bodyUrsache: Die Funktion übergibt dem Closure mehr Werte, als es annimmt. sorted(by:) übergibt immer zwei Elemente.
Lösung: Nimm alle Parameter an, zum Beispiel { a, b in a > b } oder kurz { $0 > $1 }.
Mehr dazu in der Lektion Funktionen und Closures.
Code und vollständige Ausgabe
closure-parameter.swift Swift 6.4.0, Linux x86-64, 19.10.2026 let zahlen = [3, 1, 2] let sortiert = zahlen.sorted { a in a > 0 }Compiler-Meldung
closure-parameter.swift:2:32: error: contextual closure type '(Int, Int) throws -> Bool' expects 2 arguments, but 1 was used in closure body 1 | let zahlen = [3, 1, 2] 2 | let sortiert = zahlen.sorted { a in a > 0 } | `- error: contextual closure type '(Int, Int) throws -> Bool' expects 2 arguments, but 1 was used in closure body 3 | -
Struct-Methode ändert Eigenschaft ohne mutating
Compilerfehler
error: left side of mutating operator isn't mutable: 'self' is immutableUrsache: Methoden eines Structs dürfen dessen Eigenschaften nur ändern, wenn sie mit mutating markiert sind.
Lösung: Schreibe mutating vor func.
Mehr dazu in der Lektion Structs, Klassen, Enums.
Code und vollständige Ausgabe
mutating-fehlt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 struct Zaehler { var stand = 0 func erhoehe() { stand += 1 } }Compiler-Meldung
mutating-fehlt.swift:5:15: error: left side of mutating operator isn't mutable: 'self' is immutable 2 | var stand = 0 3 | 4 | func erhoehe() { | `- note: mark method 'mutating' to make 'self' mutable 5 | stand += 1 | `- error: left side of mutating operator isn't mutable: 'self' is immutable 6 | } 7 | } -
Eigenschaft in einer nicht-mutating Methode zugewiesen
Compilerfehler
error: cannot assign to property: 'self' is immutableUrsache: Eine normale Methode eines Structs weist einer Eigenschaft einen neuen Wert zu. In SwiftUI passiert das oft, wenn eine View eine normale Eigenschaft ändern will.
Lösung: Markiere die Methode mit mutating. In einer SwiftUI-View gehört der Wert stattdessen in @State.
Mehr dazu in der Lektion Structs, Klassen, Enums.
Code und vollständige Ausgabe
self-immutable.swift Swift 6.4.0, Linux x86-64, 19.10.2026 struct Anzeige { var titel = "Lernliste" func umbenennen() { titel = "Neu" } }Compiler-Meldung
self-immutable.swift:5:9: error: cannot assign to property: 'self' is immutable 2 | var titel = "Lernliste" 3 | 4 | func umbenennen() { | `- note: mark method 'mutating' to make 'self' mutable 5 | titel = "Neu" | `- error: cannot assign to property: 'self' is immutable 6 | } 7 | } -
mutating-Methode auf einer Konstante aufgerufen
Compilerfehler
error: cannot use mutating member on immutable value: 'z' is a 'let' constantUrsache: Ein Struct in einer let-Konstante ist vollständig unveränderlich, auch über mutating-Methoden.
Lösung: Lege die Variable mit var an.
Mehr dazu in der Lektion Structs, Klassen, Enums.
Code und vollständige Ausgabe
let-mutating.swift Swift 6.4.0, Linux x86-64, 19.10.2026 struct Zaehler { var stand = 0 mutating func erhoehe() { stand += 1 } } let z = Zaehler() z.erhoehe()Compiler-Meldung
let-mutating.swift:7:3: error: cannot use mutating member on immutable value: 'z' is a 'let' constant 4 | } 5 | 6 | let z = Zaehler() | `- note: change 'let' to 'var' to make it mutable 7 | z.erhoehe() | `- error: cannot use mutating member on immutable value: 'z' is a 'let' constant 8 |
-
Typ erfüllt ein Protokoll nicht
Compilerfehler
error: type 'Kreis' does not conform to protocol 'Form'Ursache: Eine Anforderung des Protokolls fehlt oder hat eine andere Signatur. Die note zeigt, welche.
Lösung: Ergänze die fehlende Eigenschaft oder Methode mit genau der verlangten Signatur.
Mehr dazu in der Lektion Protokolle und Generics.
Code und vollständige Ausgabe
nicht-konform.swift Swift 6.4.0, Linux x86-64, 19.10.2026 protocol Form { func flaeche() -> Double } struct Kreis: Form { let radius: Double }Compiler-Meldung
nicht-konform.swift:5:8: error: type 'Kreis' does not conform to protocol 'Form' 1 | protocol Form { 2 | func flaeche() -> Double | `- note: protocol requires function 'flaeche()' with type '() -> Double' 3 | } 4 | 5 | struct Kreis: Form { | |- error: type 'Kreis' does not conform to protocol 'Form' | `- note: add stubs for conformance 6 | let radius: Double 7 | } -
Generischer Typ ohne passende Einschränkung
Compilerfehler
error: referencing operator function '>' on 'Comparable' requires that 'T' conform to 'Comparable'Ursache: Eine generische Funktion vergleicht Werte, aber der Platzhaltertyp T ist nicht auf Comparable eingeschränkt.
Lösung: Schreibe die Einschränkung dazu: func groesser<T: Comparable>(…).
Mehr dazu in der Lektion Protokolle und Generics.
Code und vollständige Ausgabe
ohne-einschraenkung.swift Swift 6.4.0, Linux x86-64, 19.10.2026 func groesser<T>(_ a: T, _ b: T) -> T { a > b ? a : b }Compiler-Meldung
ohne-einschraenkung.swift:2:7: error: referencing operator function '>' on 'Comparable' requires that 'T' conform to 'Comparable' 1 | func groesser<T>(_ a: T, _ b: T) -> T { 2 | a > b ? a : b | `- error: referencing operator function '>' on 'Comparable' requires that 'T' conform to 'Comparable' 3 | } 4 | Swift.Comparable:1:17: note: where 'Self' = 'T' 1 | public protocol Comparable : Equatable, ~Copyable, ~Escapable { | `- note: where 'Self' = 'T' 2 | static func < (lhs: borrowing Self, rhs: borrowing Self) -> Bool 3 | static func <= (lhs: borrowing Self, rhs: borrowing Self) -> Bool -
Warnung: Protokoll als Typ ohne any
Warnung
warning: use of protocol 'Behaelter' as a type must be written 'any Behaelter'; this will be an error in a future Swift language modeUrsache: Ein Protokoll mit zugehörigem Typ wird als Typ verwendet. Swift 6.4 warnt und kündigt einen Fehler in einem künftigen Sprachmodus an.
Lösung: Schreibe any vor den Protokollnamen, oder mach die Funktion generisch.
Mehr dazu in der Lektion Protokolle und Generics.
Code und vollständige Ausgabe
ohne-any.swift Swift 6.4.0, Linux x86-64, 19.10.2026 protocol Behaelter { associatedtype Inhalt var inhalt: Inhalt { get } } func zeigeAlle(_ behaelter: [Behaelter]) { print(behaelter.count) }Compiler-Meldung
ohne-any.swift:6:30: warning: use of protocol 'Behaelter' as a type must be written 'any Behaelter'; this will be an error in a future Swift language mode [#ExistentialAny] 4 | } 5 | 6 | func zeigeAlle(_ behaelter: [Behaelter]) { | `- warning: use of protocol 'Behaelter' as a type must be written 'any Behaelter'; this will be an error in a future Swift language mode [#ExistentialAny] 7 | print(behaelter.count) 8 | } [#ExistentialAny]: <https://docs.swift.org/compiler/documentation/diagnostics/existential-any> ohne-any.swift:6:30: warning: use of protocol 'Behaelter' as a type must be written 'any Behaelter'; this will be an error in a future Swift language mode [#ExistentialAny] 4 | } 5 | 6 | func zeigeAlle(_ behaelter: [Behaelter]) { | `- warning: use of protocol 'Behaelter' as a type must be written 'any Behaelter'; this will be an error in a future Swift language mode [#ExistentialAny] 7 | print(behaelter.count) 8 | } [#ExistentialAny]: <https://docs.swift.org/compiler/documentation/diagnostics/existential-any> -
Werfender Aufruf ohne try
Compilerfehler
error: call can throw but is not marked with 'try'Ursache: Die Funktion ist mit throws markiert, der Aufruf aber nicht mit try.
Lösung: Schreibe try, try? oder, nur wenn ein Fehler unmöglich ist, try! vor den Aufruf.
Mehr dazu in der Lektion Fehlerbehandlung.
Code und vollständige Ausgabe
try-fehlt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 enum NetzFehler: Error { case offline } func lade() throws -> String { throw NetzFehler.offline } let text = lade()Compiler-Meldung
try-fehlt.swift:7:12: error: call can throw but is not marked with 'try' 5 | } 6 | 7 | let text = lade() | |- error: call can throw but is not marked with 'try' | |- note: did you mean to use 'try'? | |- note: did you mean to handle error as optional value? | `- note: did you mean to disable error propagation? 8 |
-
Fehler wird nirgends behandelt
Compilerfehler
error: errors thrown from here are not handledUrsache: Du rufst mit try auf, bist aber weder in einem do-Block noch in einer Funktion, die selbst throws ist.
Lösung: Umschließe den Aufruf mit do und catch, oder markiere die umgebende Funktion mit throws.
Mehr dazu in der Lektion Fehlerbehandlung.
Code und vollständige Ausgabe
nicht-behandelt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 enum NetzFehler: Error { case offline } func lade() throws -> String { throw NetzFehler.offline } func zeige() { let text = try lade() print(text) }Compiler-Meldung
nicht-behandelt.swift:8:16: error: errors thrown from here are not handled 6 | 7 | func zeige() { 8 | let text = try lade() | `- error: errors thrown from here are not handled 9 | print(text) 10 | } -
async-Aufruf ohne await
Compilerfehler
error: expression is 'async' but is not marked with 'await'Ursache: Die aufgerufene Funktion ist async. Jeder Aufruf muss die mögliche Pause mit await markieren.
Lösung: Schreibe await vor den Aufruf.
Mehr dazu in der Lektion async/await.
Code und vollständige Ausgabe
await-fehlt.swift Swift 6.4.0, Linux x86-64, 19.10.2026 func ladeZahl() async -> Int { 42 } func zeige() async { let zahl = ladeZahl() print(zahl) }Compiler-Meldung
await-fehlt.swift:4:16: error: expression is 'async' but is not marked with 'await' 2 | 3 | func zeige() async { 4 | let zahl = ladeZahl() | |- error: expression is 'async' but is not marked with 'await' | `- note: call is 'async' 5 | print(zahl) 6 | } -
async-Code in einer normalen Funktion
Compilerfehler
error: 'async' call in a function that does not support concurrencyUrsache: await ist nur in einer async-Funktion oder einem Task erlaubt.
Lösung: Markiere die umgebende Funktion mit async, oder starte den Code in einem Task { }.
Mehr dazu in der Lektion async/await.
Code und vollständige Ausgabe
kein-async.swift Swift 6.4.0, Linux x86-64, 19.10.2026 func ladeZahl() async -> Int { 42 } func zeige() { let zahl = await ladeZahl() print(zahl) }Compiler-Meldung
kein-async.swift:4:22: error: 'async' call in a function that does not support concurrency 1 | func ladeZahl() async -> Int { 42 } 2 | 3 | func zeige() { | `- note: add 'async' to function 'zeige()' to make it asynchronous 4 | let zahl = await ladeZahl() | `- error: 'async' call in a function that does not support concurrency 5 | print(zahl) 6 | } -
Mögliches Datenrennen in parallelem Code
Compilerfehler
error: passing closure as a 'sending' parameter risks causing data races between code in the current isolation context and concurrent execution of the closureUrsache: Ein Closure, das parallel laufen soll, fängt veränderlichen Zustand ein, den auch anderer Code sieht. Swift 6 lehnt das beim Übersetzen ab.
Lösung: Schütze den Zustand mit einem Actor, nutze einen Werttyp, oder gib Ergebnisse aus der Task zurück, statt von innen zu schreiben.
Mehr dazu in der Lektion async/await.
Code und vollständige Ausgabe
datenrennen.swift Swift 6.4.0, Linux x86-64, 19.10.2026 final class Zaehler { var stand = 0 } func zaehleParallel() async { let zaehler = Zaehler() await withTaskGroup(of: Void.self) { gruppe in for _ in 1...10 { gruppe.addTask { zaehler.stand += 1 } } } print(zaehler.stand) }Compiler-Meldung
datenrennen.swift:9:20: error: passing closure as a 'sending' parameter risks causing data races between code in the current isolation context and concurrent execution of the closure [#RegionIsolation::SendingClosureRisksDataRace] 7 | await withTaskGroup(of: Void.self) { gruppe in 8 | for _ in 1...10 { 9 | gruppe.addTask { zaehler.stand += 1 } | | `- note: closure captures 'zaehler' which is accessible to code in the current isolation context | `- error: passing closure as a 'sending' parameter risks causing data races between code in the current isolation context and concurrent execution of the closure [#RegionIsolation::SendingClosureRisksDataRace] 10 | } 11 | } [#RegionIsolation]: <https://docs.swift.org/compiler/documentation/diagnostics/region-isolation> [#SendingClosureRisksDataRace]: <https://docs.swift.org/compiler/documentation/diagnostics/sending-closure-risks-data-race> -
Globale Variable aus einer Funktion geändert
Compilerfehler
error: main actor-isolated var 'bestanden' can not be mutated from a nonisolated contextUrsache: Im Sprachmodus 6 gehören globale veränderliche Variablen in Skripten und main.swift zum Main Actor. Eine Funktion ohne diese Zuordnung darf sie nicht ändern.
Lösung: Kapsle den Zustand in einem Struct oder gib Werte zurück, statt eine globale Variable zu verändern.
Mehr dazu in der Lektion Projekt Wortzähler.
Code und vollständige Ausgabe
global-var.swift Swift 6.4.0, Linux x86-64, 19.10.2026 var bestanden = 0 func pruefe(_ bedingung: Bool) { precondition(bedingung) bestanden += 1 }Compiler-Meldung
global-var.swift:5:5: error: main actor-isolated var 'bestanden' can not be mutated from a nonisolated context 1 | var bestanden = 0 | `- note: mutation of this var is only permitted within the actor 2 | 3 | func pruefe(_ bedingung: Bool) { | `- note: add '@MainActor' to make global function 'pruefe' part of global actor 'MainActor' 4 | precondition(bedingung) 5 | bestanden += 1 | `- error: main actor-isolated var 'bestanden' can not be mutated from a nonisolated context 6 | } 7 |