Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App with Swift Improving Our User Interface Random Colors

Nathalie Dory
Nathalie Dory
2,912 Points

Why do you add () after declaring a property and method in a class?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var quickGermanLabel: UILabel!

let factProvider = FactProvider() let colorProvider = backgroundColorProvider()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    quickGermanLabel.text = factProvider.randomFact()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func showNewFact() {

    let newColor = UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0)

    view.backgroundColor = newColor

quickGermanLabel.text = factProvider.randomFact()

}

}

1 Answer

For anyone curious about this still, I added a more detailed answer to the same question on a different post earlier today :thumbsup: Hopefully that can help!

In short, when creating an instance of an object (class, struct, etc.), you must initialize it with values for the object's stored properties. When the class/struct has already been given default values (as in the case of this video — the structs both have an array with values), you just leave the parentheses empty (unless you want to provide alternative values for some reason).

For example:

// Given the following struct ('Vehicle') create an instance of 'Vehicle', and store it in a constant named 'myCar':

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

let myCar = Vehicle()

For more, you may want to review classes and structures in the Swift Documentation to clear up any remaining questions about creating instances. Happy Coding! :v: