|
Hello, I have some code, which retrieves JSON data from a URL and shows it in a List. This is working so far. What I try to do is to add a search function. I've tried to follow Paul's tutorial https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data without success. I can't figure out how to implement that in my code. Here is my code: Part 1:
Part 2:
Can someone please help me to implement the search function? Thanks in advance Kay |
|
Step 1: Don't over complicate your issue. Your first task is to pull a bunch of data out of the internet as a JSON stream and turn it into an array of structs that you can use. I trust you've done this. However, I think this line of code is very confusing!
Step 2: I'd suggest a new strategy here. Perhaps you should name your array allMachines. Or companyMachines. Pick a better name. Step 3:
Now that you have an array of allMachines, stop calling it JSON data. It's not JSON data anymore, it's an array of Step 4: List the machines in your user interface. But consider your two options. (1) If you have NO search terms, you want to show allMachines. (2) If you have a searchTerm, you want to show matchingMachines. Step 5:
Write the code to define matchingMachines.
Whenever your user changes the searchTerm, this computed property is recalculated. Step 6: Modify your user interface to display each machine in the matchingMachine array.
Step 8: Please let us know how you implemented this! |
|
@Obelix Thanks for your fast help. I've got it. It is working now. I still need to do Step 6. After it is done, I will post my final code. Kay |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.