Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Project 2 -- I'm a bit stuck...

Could someone please have a look at my TrueFalseApp project? I've been working through my code and have a working function - "func checkAnswer". The code works but the app crashes in Simulator when buttons are clicked. I don't know where to go from here. Any help appreciated! p.s. I have read and studied further on connecting buttons with IBActions etc, but still need help.

1 Answer

Hey Benjamin,

It looks you got kind of lost designing your data model. Think of it this way:

  • A Trivia is something that owns a list of Questions and maybe some other data like a title
  • A Question is something that owns a list of Answers, a pointer to the correctAnswer and, of course, the content or title of the question
  • An Answer can be a relatively simple thing that just contains a value

Thinking about your class/structures in such a logical way makes it way easier to draw relationships between them and, later on, assign responsibilities and identify collaborators . For example, your Trivia structure can contain an array of Question structures; a Question structure can be modeled as an array of Answer structures and so on. An array is a better underlying data-store here — better than hardcoding "answer1, answer2, ..." as you were doing — because your use-case essentially calls for each structure to have a variable list of items, all of which need to be accessed in constant time.

That should give you a little push in the right direction regarding your model layer, but your view layer is also a bit chaotic. The best way to proceed with your views, in this case, is to take full advantage of the libraries that UIKit exposes. From your layout, it seems like you want to display the answers to a given question in a vertical-row pattern; the UITableView is the bread-and-butter for this kind of thing. For displaying your list of questions for a given trivia, you'll probably want to use something like a UIPageView so you can — either programmatically or at the user's behest — swipe back-and-forth between questions.

In summary, you've made a decent attempt so far. Your biggest mistake has been choosing to hardcode your values and structures. Going forward, I recommend you take out a sheet of blank paper and try and illustrate the relationships and interactions between all the objects in your app. This is a relatively cheap way of gaining a mental-model of your codebase, the payoffs of which are immense.

Let me know if you have any further questions :)