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

Tobi Tron
Tobi Tron
2,737 Points

Why create an object at all for Simple iPhone app data model?

Rewrote this question to make it clearer:

Pasan explains why he chose to use a struct over a class, but I don't understand why an object is necessary in the first place?

I tried removing the array from the struct such that my FactModel.swift file contains just an array of facts, and a function that returns a random fact from the array. No struct. Back in the ViewController I set the label's text like so:

funFactLabel.text = getRandomFact()

It worked just fine. So I'm wondering, why the struct? Is it Swift convention to always create an object in the model even if it's not strictly necessary in case your data gets more complex down the road? Would this approach cause a bug I'm not thinking of?

Thanks!

2 Answers

Keli'i Martin
Keli'i Martin
8,227 Points

It's not so much a Swift convention as it is a best practice. It's a way of organizing your code into different parts that each perform specific functions. Sure, you could do all the work inside the ViewController if you really wanted. But if you wanted to extend your app further in the future, you could be in for a lot more work than necessary.

Tobi Tron
Tobi Tron
2,737 Points

Sorry I think I was unclear! I'm not suggesting removing the array and function from the FactModel file. 100% that shouldn't be in the view. I'm wondering why it needs to be in a struct in the model file? It would still work as an array and function that is called in the view controller. Still just as readable, so why put them in the struct?

Keli'i Martin
Keli'i Martin
8,227 Points

Are you saying just having an array and a function by themselves in the FactModel.swift file? How would the view controller access the function or array if it weren't in some kind of object that the view controller could reference? At that point it becomes a scope issue. The view controller is itself a class. Trying to call a function getRandomFact() that isn't defined in that class would cause the compiler to fail.

Tobi Tron
Tobi Tron
2,737 Points

Yeah that's what I mean. So Swift won't automatically search through files in the project and find the function of that name? I tried that and it worked, but maybe i was actually doing something different.

Will check again, thanks for the answer!

Tobi Tron
Tobi Tron
2,737 Points

Hey Keli'i I'm not sure what's going on under the hood but I did what I described and it compiled and worked just fine. I rewrote my original question to make it clearer what I'm asking/doing.

Keli'i Martin
Keli'i Martin
8,227 Points

My apologies. I didn't think that would actually compile like that. I guess I've just never thought to organize my code in such a way. In object-oriented programming, you work with objects, whether they are in the same file or many different files. Under the hood, I can't say for sure what relationship those loose functions and array have with the rest of the files in the project. I've read somewhere that because they are all a part of the same package, that is why it works. My best guess is that the function and array are seen as global in scope, which is why it works. I would still say that packaging those functions and arrays in some sort of object is just a cleaner way to code.

Tobi Tron
Tobi Tron
2,737 Points

Yeah that makes sense. Well the real reason I'm asking is I'm coming at Swift from a Ruby background, and in this situation in Rails you wouldn't create a class or something (no structs in Ruby) in the model, you'd just stick it an array and write a function to use in the controller.