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 Object-Oriented Swift Properties Computed Properties

Double naming a variable/type

It may not be in relation with this video , but I was wondering, only to clarify this once and for all.

When calling an init in the subclass, Amit wrote :

init (title: String, price: Double, height: Double, width: Double, length: Double) {

self.height = height
....
....
....
super.init(title: title, price: price)

}

What does the two "title" represents, the first and second one.

Could somenone please help me with this.

Thanks

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Jean-Emrick Rioux,

Firstly, I've fixed up your code formatting so I recommend you have a read of Posting Code to the Forum for future knowledge :smile:.

Ok, so let's break this down, in Swift constructors for structs and/or classes have a requirement that you must specify the parameter name when creating a new instance of the object.

What does that mean and what is a parameter name?

In Swift there is a core concept which is when creating a new instance of an object that you need to specify the parameter name first which is the name given in the struct and/or class init function, however if we don't specify a custom init function the names of the stored properties are used instead.

Example 1

Without a custom constructor.

struct Person {
  var firstName: String
  var lastName: String
}

var john = Person(firstName: "John", lastName: "Doe")
Example 2

With a custom constructor.

struct Person {
  var firstName: String
  var lastName: String

  init(first: String, last: String) {
    self.firstName = first
    self.lastName = last
  }
}

var john = Person(first: "John", last: "Doe")

So what's the difference, well back to your original question we can see that when creating a new instance of the Person struct it referenced the name of the stored properties when entering the arguments, whereas in the second example we've switched this to a custom constructor where the arguments now use first and last as the labels.

Ok, what's a parameter label, where'd that come from?

Well, they've been there the whole time. In Swift only constructors as I said have a requirement that you need to specify the parameter name before the value, these are called parameter labels as they tell the Swift compiler which value is for which parameter.

Parameters labels aren't just available to constructors either, you can use them anywhere but rather than explaining it all over again have a read of this post as it explains more about them in detail.

Wrapping up

  • When creating a new instance of a struct or class the parameter name is required
  • Parameter labels can be used on custom functions and methods
  • Parameter labels aren't the same as parameter values
  • Keep practicing

Hope that helps.

Thank you very much for your detailed answer. Helped me clarify a lot of this stuff.

But referring back to my first post, on the "super.init" line , there is 2 "title"

What does the first one represents , and the second.

I do not need a long complicated answer even tough it is appreciated , only both names example : variable,type

thanks