{"id":3071,"date":"2020-09-29T15:46:20","date_gmt":"2020-09-29T15:46:20","guid":{"rendered":"http:\/\/4310.mynmi.net\/?page_id=3071"},"modified":"2026-04-23T21:20:48","modified_gmt":"2026-04-23T21:20:48","slug":"random-trivia-api","status":"publish","type":"page","link":"https:\/\/nmi.cool\/appdev\/random-trivia-api\/","title":{"rendered":"Random Trivia API"},"content":{"rendered":"<p>In this assignment, you&#8217;ll be working with a Random Trivia API: <a href=\"https:\/\/the-trivia-api.com\/v2\/questions\/\">https:\/\/the-trivia-api.com\/v2\/questions\/<\/a><\/p><p>Begin by pasting it into your browser and taking a look at the array of JSON objects that appear. Each object includes a question and a correct answer, along with additional information, including an array of incorrect answers.<\/p><p>Refresh your browser and you should see a new set of &#8216;trivia&#8217; objects.<\/p><h2 class=\"wp-block-heading\">Your Goal<\/h2><p>Draw upon everything you learned in the previous <strong><a href=\"https:\/\/nmi.cool\/appdev\/lists-with-swiftui\/\">Lists with SwiftUI<\/a><\/strong> and<strong> <a href=\"https:\/\/nmi.cool\/appdev\/homework-9-working-with-apis\/\" data-type=\"page\" data-id=\"3820\">Working With APIs<\/a><\/strong> exercises to create:<\/p><ol class=\"wp-block-list\"><li>a List that displays the questions, that also includes<\/li><li>a NavigationView and NavigationLinks to the questions so that the correct answer appears in a new view when a question is clicked.<\/li><\/ol><p><strong>Don&#8217;t panic!<\/strong> The previous two lessons contain everything that you need to accomplish this assignment, plus there is a helpful trail of breadcrumbs below to help you get through this.<\/p><h2 class=\"wp-block-heading\">To begin, <\/h2><p>copy the two structs shown below and paste them at the bottom of your contentView file. Make sure they are not inside of another struct! <\/p><pre class=\"wp-block-code\"><code>\/\/ MARK: - QuizItem\nstruct QuizItem: Codable, Identifiable {\n    let category: String\n    let id: String\n    let correctAnswer: String\n    let incorrectAnswers: [String]\n    let question: Question\n    let tags: [String]\n    let type: String\n    let difficulty: String\n    let regions: [String]\n    let isNiche: Bool\n}\n\n\/\/ MARK: - Question\nstruct Question: Codable {\n    let text: String\n}<\/code><\/pre><p>You could also put these at the top as in <a href=\"https:\/\/nmi.cool\/appdev\/homework-9-working-with-apis\/\" data-type=\"page\" data-id=\"3820\">Working with APIs<\/a>, the location doesn&#8217;t matter \ud83d\ude42<\/p><h2 class=\"wp-block-heading\">Hints\/Tips<\/h2><ol class=\"wp-block-list\"><li>Duplicate the\u00a0<strong>Working With APIs<\/strong>\u00a0project folder and use the copied project for this assignment.<\/li><li>Especially if you&#8217;re using Xcode 16 or newer, add <code>import Combine<\/code> to the top of your file.<\/li><li>Replace the url that leads to the Pokemon data with<strong> <\/strong><a href=\"https:\/\/the-trivia-api.com\/v2\/questions\/\"><strong>https:\/\/the-trivia-api.com\/v2\/questions\/<\/strong><\/a><\/li><li>Replace the Pokemon and PokemonRequest struct with the two shown above, named QuizItem and Question. You are welcome to copy and paste them. <ol start=\"5\" class=\"wp-block-list\"><li>Then, replace @Published <strong>var<\/strong> pokemonList: [Pokemon] = []<span class=\"s3\" style=\"font-size: revert\"> with <\/span><\/li><\/ol><\/li><\/ol><pre class=\"wp-block-code\"><code>@Published var quizList: [QuizItem] = []<\/code><\/pre><ol start=\"6\" class=\"wp-block-list\"><li>Next, update the do statement by replacing it with\u00a0<\/li><\/ol><pre class=\"wp-block-code\"><code>do {\n                \/\/ Decode the JSON into your model\n                let decodedResponse = try JSONDecoder().decode([QuizItem].self, from: data)\n                DispatchQueue.main.async {\n                    self.quizList = decodedResponse\n                }\n            }<\/code><\/pre><ol start=\"7\" class=\"wp-block-list\"><li>In ContentView, you&#8217;ll need to update the List so instead of being List(viewModel.pokemonList) { pokemon <strong>in<\/strong>, it reads<\/li><\/ol><pre class=\"wp-block-code\"><code>List(viewModel.quizList) { QuizItem in<\/code><\/pre><ol start=\"8\" class=\"wp-block-list\"><li>Finally, replace the Text with <\/li><\/ol><pre class=\"wp-block-code\"><code>Text(QuizItem.question.text.isEmpty ? \"No Question Available\" : QuizItem.question.text)<\/code><\/pre><p>We&#8217;ve added a catch statement to make sure the row is filled even if data is missing<\/p><ol start=\"9\" class=\"wp-block-list\"><li>You may need to change a few more variables, but take them one at a time and try to change all instances of pokemon with QuizItem, for example.<\/li><\/ol><h2 class=\"wp-block-heading\">Add Navigation to the Correct Answer<\/h2><p>Refer to the Lists with SwiftUI lesson to figure out how to create a <code>NavigationLink<\/code> to the correct answer (<code>correctAnswer<\/code>). You can do this!<\/p><h2 class=\"wp-block-heading\">What else can I do?<\/h2><p>Look at the feed that you are using. Each object contains, in addition to a question and a correct answer, 3 incorrect answers. Give some thought to how you could turn this whole thing into a multiple-choice exercise with scoring. <\/p><p>Also, note that each object comes with a category. You can easily generate a custom feed for a specific category by appending, for example,<br><em>?categories=film_and_tv<\/em> or <em>?categories=history<\/em> to your URL, like this:<br><a href=\"https:\/\/the-trivia-api.com\/v2\/questions\/?categories=history\">https:\/\/the-trivia-api.com\/v2\/questions\/?categories=history<\/a>. You can call more than one category by adding commas like this:<br><a href=\"https:\/\/the-trivia-api.com\/v2\/questions\/?categories=history,geography\">https:\/\/the-trivia-api.com\/v2\/questions\/?categories=history,geography<\/a><\/p><h2 class=\"wp-block-heading\">Don&#8217;t forget to make this app your own!<\/h2><p>Did you&#8230;<\/p><ul class=\"wp-block-list\"><li>add a title?<\/li><li>add a fun background?<\/li><li>edit the text?<\/li><li>see if there&#8217;s anything else you want to add to make your app better? (ex. make your app refreshable)<\/li><\/ul><p>Put everything together that you&#8217;ve covered so far and see if you can add some personality to your very own random trivia app. Don&#8217;t be afraid to do some Googling at this point as you play around with your style.<\/p><h2 class=\"wp-block-heading\">Rubric<\/h2><p><strong>A-Level Work<\/strong><\/p><ul class=\"wp-block-list\"><li>Exceptional quality, originality, and\/or insight<\/li><li>Fully meets criteria with no significant errors<\/li><li>Polished, organized, and professional<\/li><\/ul><p><strong>B-Level Work<\/strong><\/p><ul class=\"wp-block-list\"><li>Meets all criteria with minor issues<\/li><li>Solid understanding and effort<\/li><li>Good organization and presentation<\/li><\/ul><p><strong>C-Level Work<\/strong><\/p><ul class=\"wp-block-list\"><li>Meets most criteria; notable gaps<\/li><li>Basic or inconsistent understanding<\/li><li>Organization or clarity needs work<\/li><\/ul><p><strong>Below C-Level (Not Passing)<\/strong><\/p><ul class=\"wp-block-list\"><li>Disorganized, incomplete, or poor quality<\/li><li>Meets few or no criteria; major errors or omissions<\/li><li>Limited or no understanding and effort<\/li><\/ul>","protected":false},"excerpt":{"rendered":"<p>In this assignment, you&#8217;ll be working with a Random Trivia API: https:\/\/the-trivia-api.com\/v2\/questions\/ Begin by pasting it into your browser and taking a look at the array of JSON objects that appear. Each object includes a question and a correct answer, along with additional information, including an array of incorrect answers. Refresh your browser and you &hellip; <a href=\"https:\/\/nmi.cool\/appdev\/random-trivia-api\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Random Trivia API<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3071","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/3071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/comments?post=3071"}],"version-history":[{"count":25,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/3071\/revisions"}],"predecessor-version":[{"id":4267,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/3071\/revisions\/4267"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/media?parent=3071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}