|
Trying to search the json data and display the result of search but I can't figure out how to create an array based on the desired search term. Here's what I'm thinking should work... @objc func filterData() { let ac = UIAlertController(title: "Filter petitions by:", message: nil, preferredStyle: .alert) ac.addTextField() let submitAction = UIAlertAction(title: "Search", style: .default) { [weak self, weak ac] action in guard let answer = ac?.textFields?[0].text else { return } self?.submit(answer) } ac.addAction(submitAction) present(ac, animated: true) }
|
|
You need to filter the results array. So something like:
|
|
Thanks, that got me past the compiler error but now my alert for getting the seach string doesn't appear to be working. @objc func credits() { let ac = UIAlertController(title: "Credits", message: "Data from the White Houses We the People API (answer)", preferredStyle: .alert) present(ac, animated: true) }
So this should show whatever is entered for searching in another alert but it doesn't and I can't get rid of the alert from the credits(). |
BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
You don't add any buttons to your credits alert so there's no way to dismiss it. You need something like:
Would need to see more of your code—especially the |
|
|
|
@macinlew - have you resolved this problem ? I also got stuck on this .. If you resolved this, could you share your code ? |
|
Hi! I have solved it but not in the way i would have wanted, and i don't understand part of it. Hope it helps you to solve yours: In the viewController class i created two additional arrays: One that saves all the petitions (to reload them again later). One with the filtered petitions. Then created a button using interface builder (was easier for my to configure the image of the magnifier there 😖). After that i defined the submit func inside viewDidLoad. By the way, what roosterboy says, seems to be much more efficient than my for loop!
Inside viewController Class:
Inside view did load
But i have some questions: First, how could i make it to reload everything when tapping the bottom tab, instead of having a button in the UIAlertController? I think it is more intuitive. Second, why do i have to put the [weak self] action in line in clearSearch for it to work? Then, how could i personalize the top buttons with a system image from SF symbols, without using the interface builder? There i used the questionMark for credits and the magnifier for the search button. Finally, is there an easy way to change the array that is shown, instead of changing the content of one array? Let me know if it helped you to solve it, if you would improve something in there, and thanks in advance for helping me!. 😁 |
|
|
|
Don't worry! I gave up on a couple of challenges too, but im sure that as we keep learning, they will become easier for us. |
|
Guys it's pretty straightforward, this is what I did. Of course I did not show the other functions not related to this question. And also do not forget to use your filteredPetitions array as your data for tableview.
|
|
The Has anyone else experienced this? This is my code in
And this is the output I am getting:
|
|
Also if you guys use the "filteredPetitions" array in this method, than you will be able to filter (damn this challenge is hard even me I've had to come on here, first I thought I needed 3 arrays, this was not easy but even if we look at each others code we still learn because at some point you can be on one challenge long time and still you did not figured it out then you need help because the main thing here is to understand the code and logic, overtime we all going to have a better logic and will be easier.) override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return filteredPetitions.count } |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
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.