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 trialLana Wong
3,968 PointsWhy can't you directly assign the FactProvider class to FactLabel text in the view controller?
Hi, I have not been here for a little while.
QUESTION: Why do you have to assign "Factprovider" to a new variable called "factProvider" in the view controller? Why does Swift not let you write:
FactLabal.text = Factprovider.randomFact()?
Thanks :)
Lana
3 Answers
Maria Angelica Dadalt
6,197 PointsHi again. I went searching for the answer and I found it in the Swift Language Guide. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html Classes and Structs need instances (variables and constants) to give value to the properties inside them. Since you can use Classes and Structs multiple times, the use of constants and variables to access those objects can give you the flexibility to create many instances of the same Class or Struct. I hope this can help you. I recommend you read the guide. It helps a lot!
Maria Angelica Dadalt
6,197 PointsHi, I haven't been here long either, but I think it's because you are trying to access an object from another file and you can only do that if you assign it to a constant or variable.
I'll be sure to watch what others answer you so I can find this out for myself too!
Jeff McDivitt
23,970 PointsMaria Angelica Dadalt is correct you need to access it from another file so you need to create a constant or variable. If I am not mistaken this is the lesson where Pasan starts explaining how to build projects using the MVC (Model View Controller) method; instead of putting everything in your view controller.
Lana Wong
3,968 PointsLana Wong
3,968 PointsThanks you so much! You explanation really helped! :)