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 trialNathalie Dory
2,912 PointsWhy 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
kols
27,007 PointsFor anyone curious about this still, I added a more detailed answer to the same question on a different post earlier today 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!