Skip to content
0:00 / 0:00

#swiftui#propertywrapper#debounce

SwiftUI Tutorial: Debouncing User Input in a Property Wrapper

CodeSlicing

2.1K subscribers • 1 video on Hallway

YouTube

Intermediate. Assumes working knowledge.

781 views4 years ago1 recommendation

Mobile developmentProgramming languagesSwiftiOS and SwiftUI

How to build a custom property wrapper in SwiftUI to easily debounce user input from a text field.

SwiftUI Tutorial: Debouncing9 User Input in a Property Wrapper In this episode, we’re going to take the debouncing work we did in the last episode and wrap it all in a property wrapper which will allow us to easily use it anywhere we like. Source code: gist.github.com/CodeSlicing/5500c4b695777f5402969c14862e94e7 Native source code: gist.github.com/CodeSlicing/026d8481dea0e4f5f5da85ea4dce6fc4 ================ PureSwiftUI is a free Swift Package that includes a multitude of useful extensions and properties to make your life easier with SwiftUI resulting in cleaner code that takes less time to write. It's a huge time-saver so I do encourage you to check it out. You can get PureSwiftUI here: github.com/CodeSlicing/pure-swift-ui If you want to ask me a question or follow me for updates, you can do so on Twitter: twitter.com/codeslice FAQs: Q: What are the code callouts I sometimes see in the code?: A: I code in SwiftUI using the PureSwiftUI framework since I created it to make code more concise and easier to read. However, I understand that not everyone wants to install a swift package (although, you'd be mad not to ;o)) so unless otherwise stated, the popups will detail the native SwiftUI equivalent of the code in question. Q: What's that theme you're using? A: It’s a modified version of MoodNight v2. If you like it, you can download the theme here: github.com/CodeSlicing/codeslicing-youtube-resources/tree/de… Q: Why do you use a harness structure rather than the default preview provider A: Please take a look at this video for a detailed explanation: youtu.be/uO17ofc5a54

Why it's here

1 person recommends this

What people say. Recommended as a fantastic property wrapper solution for debouncing text input in SwiftUI.

Antonon Stack Overflow • Recommends

On “SwiftUI: How to only run code when the user stops typing in a TextField?”

There's a fantastic PropertyWrapper solution to this by link to this video that lets you simply replace @State var text = "" with @DebouncedState var text = "" and you're done. You can also set the amount of debouncing (which defaults to 0.3 seconds, or whatever you like). For a longer delay of 1.5 seconds: @DebouncedState(delay: 1.5) var text = "" Here's his video: link to this video And his code: https://gist.github.com/CodeSlicing/026d8481dea0e4f5f5da85ea4dce6fc4