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 Build a Simple iPhone App with Swift 2.0 Structs as Data Models Structs or Classes

robert rutherford
robert rutherford
2,055 Points

What is the scope of the FactModel struct?

How does the ViewController class know anything about the FactModel struct that we created? Is there something going on under the hood in Xcode that ties together any objects you create from different files in the same project?

2 Answers

robert, sorry, missed your question. If you're coming from Objective-C and its import statements for .h files, then, yes, it's an interesting question how Swift does without them.

Here's a quote from StackOverflow:

"Let's say you're creating an app called Battlefront. When you're adding files/classes to your application, they are in fact added to your app's module, the Battlefront module. Let's say you created a class called Hero, well, your class is not only Hero, it is Battlefront.Hero but since you're using it in the context of the Battlefront module, there is no need to specify the module name when using your class."


robert, check out the video around 3:15. Pasan links the FactModel there to the ViewController with this line of code:

let factModel = FactModel()
robert rutherford
robert rutherford
2,055 Points

Thanks for the response, but it isn't clear to me how the ViewController knows about the struct that was created in another file. What if I had that struct created in a file located in a random location on the hard drive? How would the ViewController for this project know what the FactModel struct was?