Use the onChange modifier in SwiftUI

Daily Coding Tip 028

Here’s a version of yesterday’s tip that uses the onChange modifier instead of a button to add up the values. This allows you to have the two numbers add up automatically when either of them changes. The closure for the modifier gets passed the value it changed to, but since both properties are local we don’t need the new value. That’s what the _ in is for, as the passed value cannot be implicitly ignored.

Get more Daily Coding Tips in your inbox!