Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Swift Closures — Accepting Functions as Parameters 🎯

So far we've been passing closures into functions that Swift provides — like sorted(), filter(), and map(). But what if you wanted to write your own function that accepts another function as a parameter? That's exactly what we're covering t…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

So far we've been passing closures into functions that Swift provides — like sorted(), filter(), and map(). But what if you wanted to write your own function that accepts another function as a parameter? That's exactly what we're covering today. 🍥



This might sound complicated, but once you see it in action it starts to make a lot of sense — and it's everywhere in SwiftUI.









Why Would You Even Want This?



Before we write any code, let's think about why this matters.



Imagine you're building an anime battle app that needs to fetch data from a server — maybe pulling in a list of all One Piece characters. Your iPhone can do billions of things per second, but waiting for a server response can take half a second or more. That's practically glacial by comparison.



If your app just sat there waiting for the server to respond, the whole UI would freeze. Nobody wants that.



The solution? Pass in a closure that says: "go do this slow work, and when you're done, call this function with the result." Your app keeps running smoothly, and the closure fires when the data arrives. That's closures as parameters in a real-world nutshell. 🌀









Writing a Function That Accepts a Function



Let's start with a practical example. Here's a function that generates an array of random jutsu power levels by calling another function repeatedly:




func generatePowerLevels(count: Int, using generator: () -> Int) -> [Int] {
var levels = [Int]()

for _ in 0..<count {
let newLevel = generator()
levels.append(newLevel)
}

return levels
}






Let's break down what's happening on that first line:




func generatePowerLevels(count: Int, using generator: () -> Int) -> [Int]








  • count: Int — how many power levels to generate


  • using generator: () -> Int — a function parameter called generator, which takes no parameters itself but returns an Int every time it's called


  • -> [Int] — the whole generatePowerLevels function returns an array of integers



Inside the function, we just call generator() on each loop iteration, collecting its returned value into our array.



Now let's call it using a trailing closure:




let powerLevels = generatePowerLevels(count: 5) {
Int.random(in: 1...9000)
}

print(powerLevels) // e.g. [4521, 8832, 312, 7741, 999]






Swift sees the trailing closure and knows it matches the generator: () -> Int parameter — no labels needed.



You can also pass in a named function instead of a closure:




func randomChakra() -> Int {
Int.random(in: 1...9000)
}

let chakraLevels = generatePowerLevels(count: 5, using: randomChakra)
print(chakraLevels)






Both produce exactly the same result — a closure and a named function are interchangeable here, because they have the same type: () -> Int.









Reading the Function Signature



The trickiest part of accepting functions as parameters is reading the syntax. Let's slow down on it:




func generatePowerLevels(count: Int, using generator: () -> Int) -> [Int]






There are two -> arrows here, which can be confusing at first:




  • The first -> (inside () -> Int) belongs to the parameter function — it describes what the function we're passing in returns

  • The second -> (at the end, -> [Int]) belongs to our function — it describes what generatePowerLevels itself returns



Think of it like this: generator is a function that lives inside the parameter list. It has its own type, just like Int or String would — it just happens to be a function type.









Multiple Trailing Closures



Here's something that appears constantly in SwiftUI: functions that accept multiple function parameters.



Imagine an anime training sequence that needs to run three stages — warmup, training, and cool-down — each customizable:




func runTrainingArc(warmup: () -> Void, training: () -> Void, cooldown: () -> Void) {
print("🏃 Starting warmup...")
warmup()
print("⚔️ Starting training...")
training()
print("🧘 Starting cooldown...")
cooldown()
print("✅ Training arc complete!")
}






When calling a function with multiple trailing closures, the first one works exactly like before — no label, just {. But the second and third each get their label written outside the brace:




runTrainingArc {
print("Stretching and light jogging")
} training: {
print("Naruto Shadow Clone Jutsu x1000")
} cooldown: {
print("Ramen break 🍜")
}






Output:




🏃 Starting warmup...
Stretching and light jogging
⚔️ Starting training...
Naruto Shadow Clone Jutsu x1000
🧘 Starting cooldown...
Ramen break 🍜
✅ Training arc complete!






This multiple-trailing-closure syntax is something you'll see all the time in SwiftUI — for example, creating a Section with a header, content, and footer each uses a separate trailing closure.









The Function Type Cheat Sheet



When writing function parameters, here's how to read and write the types:




























What the function does Its type
Takes nothing, returns nothing () -> Void
Takes nothing, returns an Int () -> Int
Takes a String, returns nothing (String) -> Void
Takes two Strings, returns a Bool (String, String) -> Bool


The parameter list goes in the first (), and the return type goes after ->. That's all function types are — a description of what goes in and what comes out.









Why This Matters for SwiftUI



In SwiftUI, almost every component you build uses this pattern. A Button accepts a function for what happens when tapped, and a function for what to display. A List accepts a function to generate each row. Even a VStack uses a closure to hold all its child views.



Once you're comfortable with the idea of passing functions around as arguments, SwiftUI's syntax stops looking mysterious and starts reading like a natural description of your UI. That's the payoff for all this closure work. 🌸









Wrap Up




























Concept What It Means
Function as parameter You can pass a function (or closure) as an argument to another function
() -> Int A function type — takes nothing, returns an Int
Multiple trailing closures Call each extra closure with its label outside the brace
Named function vs closure Interchangeable as long as the type matches





This article was written by me; AI was used to improve grammar and readability.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Swift Closures — Accepting Functions as Parameters 🎯

Thematisch verwandte Begriffe: Swift, Closures, Accepting, Functions · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick