🕵️ SicherheitslückenCVE-2026-8070 | ASUS Armoury Crate up to 6.4.12 permission assignment(17.09.2026 um 13:05 Uhr)
🕵️ SicherheitslückenCVE-2026-8919 | ASUS GameSDK up to 1.0.5 Web cross-domain policy(17.09.2026 um 13:05 Uhr)
🕵️ Reverse EngineeringHow Candidates Could Use AI for Good(17.09.2026 um 13:06 Uhr)
🕵️ SicherheitslückenCVE-2026-8070 | ASUS Armoury Crate up to 6.4.12 permission assignment(17.09.2026 um 13:05 Uhr)
🕵️ SicherheitslückenCVE-2026-8919 | ASUS GameSDK up to 1.0.5 Web cross-domain policy(17.09.2026 um 13:05 Uhr)
🕵️ Reverse EngineeringHow Candidates Could Use AI for Good(17.09.2026 um 13:06 Uhr)
🔧 Programmierung 🕛 vor 2 Monaten 4 Min Lesezeit
0

Swift Structs — Property Observers (willSet & didSet) 👀

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

So far we've seen how to store values in properties and calculate them dynamically with computed properties. But what if you want to react every time a property changes — automatically, without having to remember to call a function yourself? That's exactly what property observers are for. 🍥



Swift gives us two flavors:





  • didSet — runs after the property changes


  • willSet — runs before the property changes









The Problem Without Property Observers



Imagine we're tracking a ninja's battle score:




CODE
struct BattleTracker {
var score = 0
}

var naruto = BattleTracker()
naruto.score += 10
print("Score is now \(naruto.score)")
naruto.score -= 3
print("Score is now \(naruto.score)")
naruto.score += 1
// Oops — forgot to print this one! 🐛






We have to remember to print the score every time it changes. Forget once — like that last line — and you've got a bug. In a small example that's easy to spot, but in a real app with dozens of places updating a property? That's a disaster waiting to happen.









didSet — React After the Change



With didSet, we attach the print directly to the property. Now it runs every time the score changes, no matter where the change happens:




CODE
struct BattleTracker {
var score = 0 {
didSet {
print("Score updated to \(score)!")
}
}
}

var naruto = BattleTracker()
naruto.score += 10 // "Score updated to 10!"
naruto.score -= 3 // "Score updated to 7!"
naruto.score += 1 // "Score updated to 8!"






No more forgotten print statements. Swift handles it automatically. 🎉






Accessing the Old Value



Inside didSet, Swift automatically gives you oldValue — the value the property had before the change. This is useful when you want to show what changed:




CODE
struct Ninja {
var chakraLevel = 1000 {
didSet {
print("Chakra changed from \(oldValue) to \(chakraLevel)")
}
}
}

var sasuke = Ninja()
sasuke.chakraLevel -= 300 // "Chakra changed from 1000 to 700"
sasuke.chakraLevel -= 150 // "Chakra changed from 700 to 550"






oldValue is automatically provided — you don't need to declare it yourself.









willSet — React Before the Change



willSet works the same way but fires before the property changes. Inside it, Swift automatically provides newValue — the value that's about to be assigned:




CODE
struct Guild {
var members = [String]() {
willSet {
print("Current members: \(members)")
print("About to change to: \(newValue)")
}

didSet {
print("Guild now has \(members.count) members.")
print("Previous list was: \(oldValue)")
}
}
}

var akatsuki = Guild()
akatsuki.members.append("Pain")
akatsuki.members.append("Konan")






Both willSet and didSet fire when you append to an array — because appending is a change to the property.









willSet vs didSet — Which Should You Use?



In practice, didSet is what you'll use most of the time. The reason is simple: you usually want to react after something has changed — update the UI, save data, log something.



willSet is less common, but it shines when you need to capture the state before the change happens. SwiftUI itself uses willSet in certain places for animations — it takes a snapshot of the UI before the change, then compares it with the "after" snapshot to figure out what needs to animate.



A quick summary:























Observer Fires Automatic value available
willSet Before the change
newValue (the value coming in)
didSet After the change
oldValue (the value that just left)








Why Use Observers Instead of Just Calling a Function?



You might be thinking: "couldn't I just call a function manually every time I update a property?" Yes — but then you have to remember to do it. Every. Single. Time.




CODE
// Without observers — easy to forget:
naruto.chakraLevel -= 300
logChakraChange() // what if you forget this line?

naruto.chakraLevel -= 150
// Forgot to call logChakraChange() here — silent bug 🐛






With a didSet observer, you only write the logic once, attached to the property itself. Swift guarantees it runs every time the property changes, no matter where in your code that happens. You're transferring the responsibility to Swift so your brain can focus on more interesting things. 🌸









One Important Warning



Keep property observers lightweight. If score += 1 looks like a simple operation, the reader of your code (including future you) will expect it to be fast. If your didSet runs an expensive network call or sorts a massive array every time a number increments, you'll get mysterious slowdowns that are hard to trace.



A good rule of thumb: property observers should do quick reactive work — logging, updating a UI label, playing a sound effect. Heavy work belongs in a function you call explicitly, where the cost is obvious. 🌸









Wrap Up
































Concept What It Means
didSet Runs after a property changes; oldValue is available
willSet Runs before a property changes; newValue is available
oldValue The value the property had before the change (inside didSet)
newValue The value about to be assigned (inside willSet)
Why observers? Swift guarantees they run — no forgetting to call a function





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

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Update-Panne bei Microsoft: Windows-11-Update sperrt Nutzer aus Domänen aus
1 Quelle
Chips und mehr: Huawei hat einen Plan für die Konkurrenz mit Nvidia
1 Quelle
Neunjähriger gibt 118.000 Dollar aus, um YouTube-Kanal zu bewerben
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Swift Structs — Property Observers (willSet & didSet) 👀

Thematisch verwandte Begriffe: Swift, Structs, Property, Observers · 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 ...