{"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-08-21T18:02:21","modified_gmt":"2026-08-21T18:02:21","slug":"random-trivia-api","status":"publish","type":"page","link":"https:\/\/nmi.cool\/appdev\/random-trivia-api\/","title":{"rendered":"Project 2: Random Trivia API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\">Refresh your browser and you should see a new set of &#8216;trivia&#8217; objects.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Your Goal<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<ol class=\"wp-block-list\">\n<li>a List that displays the questions, that also includes<\/li>\n\n\n\n<li>a NavigationView and NavigationLinks to the questions so that the correct answer appears in a new view when a question is clicked.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><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>\n\n\n\n<h2 class=\"wp-block-heading\">To begin, <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<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: &#91;String]\n    let question: Question\n    let tags: &#91;String]\n    let type: String\n    let difficulty: String\n    let regions: &#91;String]\n    let isNiche: Bool\n}\n\n\/\/ MARK: - Question\nstruct Question: Codable {\n    let text: String\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading\">Hints\/Tips<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Duplicate the&nbsp;<strong>Working With APIs<\/strong>&nbsp;project folder and use the copied project for this assignment.<\/li>\n\n\n\n<li>Especially if you&#8217;re using Xcode 16 or newer, add <code>import Combine<\/code> to the top of your file.<\/li>\n\n\n\n<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>\n\n\n\n<li>Replace the Pokemon and PokemonRequest struct with the two shown above, named QuizItem and Question. You are welcome to copy and paste them. \n<ol start=\"5\" class=\"wp-block-list\">\n<li>Then, replace @Published <strong>var<\/strong> pokemonList: [Pokemon] = []<span class=\"s3\" style=\"font-size: revert\"> with <\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>@Published var quizList: &#91;QuizItem] = &#91;]<\/code><\/pre>\n\n\n\n<ol start=\"6\" class=\"wp-block-list\">\n<li>Next, update the do statement by replacing it with&nbsp;<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>do {\n                \/\/ Decode the JSON into your model\n                let decodedResponse = try JSONDecoder().decode(&#91;QuizItem].self, from: data)\n                DispatchQueue.main.async {\n                    self.quizList = decodedResponse\n                }\n            }<\/code><\/pre>\n\n\n\n<ol start=\"7\" class=\"wp-block-list\">\n<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>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>List(viewModel.quizList) { QuizItem in<\/code><\/pre>\n\n\n\n<ol start=\"8\" class=\"wp-block-list\">\n<li>Finally, replace the Text with <\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Text(QuizItem.question.text.isEmpty ? \"No Question Available\" : QuizItem.question.text)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ve added a catch statement to make sure the row is filled even if data is missing<\/p>\n\n\n\n<ol start=\"9\" class=\"wp-block-list\">\n<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>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Add Navigation to the Correct Answer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading\">What else can I do?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading\">Don&#8217;t forget to make this app your own!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Did you&#8230;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>add a title?<\/li>\n\n\n\n<li>add a fun background?<\/li>\n\n\n\n<li>edit the text?<\/li>\n\n\n\n<li>see if there&#8217;s anything else you want to add to make your app better? (ex. make your app refreshable)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">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>\n\n\n\n<h2 class=\"wp-block-heading\">Reflection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Along with your project, push a <code>reflection.md<\/code> file (150\u2013300 words) reflecting on your process \u2014 what you learned, what was challenging, choices you made, or anything else worth noting about how the project came together. There&#8217;s no fixed set of questions to answer; write in your own words.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rubric<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>A-Level Work<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Exceptional quality, originality, and\/or insight<\/li>\n\n\n\n<li>Fully meets criteria with no significant errors<\/li>\n\n\n\n<li>Polished, organized, and professional<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>B-Level Work<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Meets all criteria with minor issues<\/li>\n\n\n\n<li>Solid understanding and effort<\/li>\n\n\n\n<li>Good organization and presentation<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>C-Level Work<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Meets most criteria; notable gaps<\/li>\n\n\n\n<li>Basic or inconsistent understanding<\/li>\n\n\n\n<li>Organization or clarity needs work<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Below C-Level (Not Passing)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Disorganized, incomplete, or poor quality<\/li>\n\n\n\n<li>Meets few or no criteria; major errors or omissions<\/li>\n\n\n\n<li>Limited or no understanding and effort<\/li>\n<\/ul>\n","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\">Project 2: 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":27,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/3071\/revisions"}],"predecessor-version":[{"id":4339,"href":"https:\/\/nmi.cool\/appdev\/wp-json\/wp\/v2\/pages\/3071\/revisions\/4339"}],"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}]}}