Day 35
Day 35 관련
Project 7, part three
In this project we built the kind of app you’ll see in thousands of different iOS job interviews around the world: here’s a JSON feed, please parse it and show in a table view. Sure, our specific version was about one feed, but the concepts – fetching data, using Codable
, rendering things into a web view, etc – are applicable anywhere.
But you’ve done more than just learn some new coding techniques. Michelle Obama once said “through my education, I didn't just develop skills, I didn't just develop the ability to learn, but I developed confidence.” And that’s true of you, too: each time you complete these projects you add another completed project to your résumé and you add another load of skills to your toolkit, but you’re also building the confidence that when you face a similar problem in the future you’ll realize you can look back on your work these last few days and have an instant refresher.
And it is a refresher. You see, you won’t remember the precise code you wrote today, and you might even forget class names like JSONDecoder
. But that’s OK: being a skilled developer isn’t about memorizing classes or methods – it’s about knowing concepts. And if you’re able to apply the concepts from this project with some concepts from projects 2, 4, and 5 to make something entirely new, then you’re doing a great job.
Today you should work through the wrap up chapter for project 7, complete its review, then work through all three of its challenges.
Wrap up
Wrap up
As your Swift skill increases, I hope you're starting to feel the balance of these projects move away from explaining the basics and toward presenting and dissecting code.
In this project you learned how to download JSON using Swift’s Data type, then use the Codable protocol to convert that data into Swift objects we defined. Working with JSON is something you're going to be doing time and time again in your Swift career, and you've cracked it in about an hour of work – while also learning about UITabBarController
, UIStoryboard
, and more.
Good job!
Review what you learned
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. It’s my job to make sure you take as much from these tutorials as possible, so I’ve prepared a short review to help you check your learning.
Click here to review what you learned in project 7.
Challenge
One of the best ways to learn is to write your own code as often as possible, so here are three ways you should try extending this app to make sure you fully understand what’s going on:
- Add a Credits button to the top-right corner using
UIBarButtonItem
. When this is tapped, show an alert telling users the data comes from the We The People API of the Whitehouse. - Let users filter the petitions they see. This involves creating a second array of filtered items that contains only petitions matching a string the user entered. Use a
UIAlertController
with a text field to let them enter that string. This is a tough one, so I’ve included some hints below if you get stuck. - Experiment with the HTML – this isn’t a HTML or CSS tutorial, but you can find lots of resources online to give you enough knowledge to tinker with the layout a little.
Hints
It is vital to your learning that you try the challenges above yourself, and not just for a handful of minutes before you give up.
Every time you try something wrong, you learn that it’s wrong and you’ll remember that it’s wrong. By the time you find the correct solution, you’ll remember it much more thoroughly, while also remembering a lot of the wrong turns you took.
This is what I mean by “there is no learning without struggle”: if something comes easily to you, it can go just as easily. But when you have to really mentally fight for something, it will stick much longer.
But if you’ve already worked hard at the challenges above and are still struggling to implement them, I’m going to write some hints below that should guide you to the correct answer.
If you ignore me and read these hints without having spent at least 30 minutes trying the challenges above, the only person you’re cheating is yourself.
Still here? OK. The second challenge here is to let users filter the petitions they see. To solve this you need to do a number of things:
- Have one property to store all petitions, and one to store filtered petitions. This means the user can filter the petitions several times and you don’t have to keep redownloading your JSON.
- At first your filtered petitions array will be the same as the main petitions array, so just assign one to the other when your data is ready.
- Your table view should load all its data from the filtered petitions array.
- You’ll need a bar button item to show an alert controller that the user can type into.
- Once that’s done, go through all the items in your petitions array, adding any you want to the filtered petition.
The important part here is the last one: how do you decide whether a petition matches the user’s search? One option is to use contains()
to check whether the petition title or body text contains the user’s search string – try it and see how you get on!
Wrap up - Additional
Once you’re done, tell other people: you’ve built another great app for iOS, and you’ve learned more about parsing JSON, tab bar controllers, and more.
You should be proud of what you’ve accomplished – keep going!