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: