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 Structs As Data Models Finishing Up Our Model

Lana Wong
Lana Wong
3,968 Points

Why do you put parentheses after the class name in the view controller?

Hi,

It's been awhile since I did this. So this may be a simple thing that I forgot.
But anyway, why do you put parentheses after the class name "FactProvider" in the view controller when creating the variable "factProvider"? Thanks

Lana

3 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

Hi,

The new .swift file that I created (named FoctProvider) to contain the facts array as well as the randomFact function is a defined as a struct.

Based on my understanding, you always have to put parentheses after the object's name when creating an instance. It is proper syntax and they're there incase you need to supply arguments for instance initialization. When creating an instance of the object, no arguments were needed thus the parentheses were empty due to the object owning no stored properties.

However if you added...

let x: Int

... to the object within your FactProvider .swift file, whether it may be class or a struct, you'll see the compiler complain that you're missing an argument for the parameter needing to be initialized in your call; the complier will also alert you for an init method if the file contains a class.

Some object instances need arguments to pertaining parameters to be initialized, some objects to not require them. In the current case, no arguments are needed, but the parentheses still have to be included during the call and if you don't include them the compiler will try to get you to fix by trying to get you to add an empty argumentlist anyway.

In one day, the object may need properties with no initial values to be hardcoded, and thus those parenthesis will be needed to hold those arguments. Also if you gave a value (e.g. 0) to the property from within the object, you will not see the compiler complain, whether it may be a class or struct.

I had this question, too, because I'd simply forgotten. Xaviar's answer above is at least partially correct, I just totally missed it when I read it through for some reason (:see_no_evil:). The following example from the Swift Documentation | Classes & Structs cleared it up for me, so I'm including an abridged version of that below. Pasan actually creates FactProvider as a struct (not a class), so I'll give an example showing this (although instantiating a class would be the same).

Here's an example struct, Vehicle:

struct Vehicle {
    var numberOfWheels = 4
    var numberOfDoors = 2
}

And here's a instance of Vehicle we're going to create:

let myCar = Vehicle()

^^ Because we provided default values (e.g., 4 and 2) for both of the stored properties in Vehicle, we didn't have to provide any values for these stored properties within the parentheses at the time we created the instance.

Now, if the original struct didn't have default values (or if we simply wanted to provide alternative values), we would've needed to initialize them when we created the instance. Here's an example of that below:

struct Vehicle {
    var numberOfWheels: Int
    var numberOfDoors: Int
}

let myBicycle = Vehicle(numberOfWheels: 2, numberOfDoors: 0)

And that's why we added an empty set of parentheses when creating an instance of FactProvider in the video/lesson (the stored property facts is created as an array that already has default values):

let factProvider = FactProvider()

Hope this helps clear it up for anyone still wondering / struggling with this concept!! :point_up::heavy_check_mark:


Jeff McDivitt
Jeff McDivitt
23,970 Points

Because you are setting the variable equal to that function

Lana Wong
Lana Wong
3,968 Points

Isn't "FactProvier" a class?