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
Alexander Certosimo
1,774 PointsDoes it make more sense to use a struct over a class when dealing with user inputted data that will be edited?
In a project i am working on i want to create a model to make things a bit cleaner. I want a user to input a question and an answer to that question that i will store in an array. I planned on putting those arrays and functions into a struct to encapsulate it from the controller. Although the more i read into value and reference types, i started to think that it is better to use a class if the data in the arrays will be edited a lot. Am i understanding this correctly?
3 Answers
Gabe Nadel
Treehouse Guest TeacherIt sounds like you could get away with either, but I'd suggest using a custom class. If for no other reason than it will be good practice for when you NEED to create custom classes down the road (when the construct would be too complex for a struct).
Gabe Nadel
Treehouse Guest TeacherWell, if you are using Core Data, you should probably create a TriviaQuestion class, which would have properties of "question" and "answer", which would both be off type string.
Alexander Certosimo
1,774 PointsOk, great. Thats what i was thinking. Just out of curiosity, if i was not using Core Data, i would have to create a question and answer property which would be an Array and would have to use tuples as well?
Gabe Nadel
Treehouse Guest TeacherNo, I think EITHER a small dictionary (one key "question", one key "answer") or a tuple would work. Then you could make an array out of those tuples or dicts.
Alexander Certosimo
1,774 PointsAwesome. I have been trying to model it as an example in my class right now, but it doesnt seem to be initializing correctly for me. Do i have this wrong? i originally was going to write an init, but was not working out for me so i just assigned a default value, but still not going as i planned. By the way, thank you for all your help.
class UserData {
var userPair:[(question:String, answer:String)] = []
}
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHey again Gabe
Thanks for the reply. I did actually want to use a custom class instead, but i am not sure of an initialize to use. What do you think would be the best course of action if i wanted to take a user input of a question and an answer and store it in my custom class? This data would also need to be editable. I originally was going to use tuples to keep the related question and answer together.
Gabe Nadel
Treehouse Guest TeacherGabe Nadel
Treehouse Guest TeacherI think you'll find examples of initialization methods for swift if you poke around a bit (on Treehouse, StackOverflow, Official Apple Sample code). Also, if you want to make sure your questions and answers persist, from one app launch to the next, make sure you take that into consideration.
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 PointsHey Gabe
For persistence i was going to use NSUserDefaults, but i dont think that is going to cut it. So now i am doing my research into Core data instead. As for initialization, i have an understanding for it, i just was a bit unsure of how to go about it for my specific situation. The reason being because i need to make sure that the related question and answer stay together. This is originally how i was going to set up properties for the class
Does that look correct to use? The thing is, i think i am going to have to use an array in this model instead. In order to store the question and answer. So am i going to have to go back to my idea of using tuples to keep the related question and answer together?