Challenge #8 Stuctures

Hello, Carolina here, good evening

I am in Challenge #8 and I am not sure if I am doing what the challenge is saying.

I don’t know if this these 2 task are correct:

1- Make all 4 properties have the private access level
2- Declare a method called getDetails which returns the value of the details property.

this is my code
//////////////////////////////////////////////////////////////////
import UIKit

struct Car {

private var make:String = "Mazda"
private var model:String = "CX9"
private var year:String = "2017"
var details:String {
    let prefix = "The car is: "
    return prefix + year + make + model
}

func getDetails() -> String {
    return (details)
}

}
/////////////////////////////////////////////////////////////////
thank you so much

Hi Carolina,

Yes, that’s great.
What you could do as an added exercise is declare an instance of the struct Car like this:

let myCar = Car()

and then call the function getDetails() like this:

print(myCar.getDetails())

That will print to the console:

This car is: 2017MazdaCX9

Note that the 3 properties year, make and model are all close together so since you are using string concatenation, you could solve that by changing the line from :

return prefix + year + make + model

to

return prefix + year + " " + make + " " + model

which puts a space between the properties

Or you could even make it a single line in the computed property like this:

return "The car is: \(year) \(make) \(model)"

which is using string interpolation.

1 Like

Hello Chris,

Thank you very much for your help. I really appreciated!!! I am learning a lot!

Have a nice day!!

Hi Chris,

I am on the challenge 8 as well. I am so confused of how the answer got ‘private var’ - I never saw that in the lesson.

So, my questions is:

  1. What does the word “private” placed before the word “var” do for that line?

  2. And why was that incorporated into the line? Please let me know - i am so confused and frustrated?

Thank you for your time and understanding. Looking forward to your answer!!! :slight_smile:

Trin

Hi Trin,

Welcome to the Code Crew community.

I’ll add the details of the challenge that Chris Ching set and then talk about the questions you asked.

Adding private to the variables you have declared prevents them from being accessed externally. This means that make, model, year and details are only known to the methods and computed variables within the struct. From that you can see that getDetails() is the only object accessible externally.

This concept may take time to sink in but let that be OK. A lot of syntax in Swift can take time to understand. We all started at the ground level in the learning curve and that includes me.

It’s also OK if you opted to look at the solution to see what needed to be done.

To prove that the function getDetails() works what you need to do is declare an instance of the struct Car and then use that instance to reference getDetails(). You could do something like this:

let myCar = Car()
print(myCar.getDetails())

So breaking that down:
myCar is a copy of the struct Car and the double closing brackets () is what is known as an initialiser.

Now you can access the available method inside the struct by saying myCar.getDetails().
The dot . is known as using dot notation which enables you to access function and properties inside of structs (and classes). The only one you can access is getDetails().

I put that inside the print() function so that there would be some output in the console below the code editor.

If you try to access any of the variables you will get a compiler error. Eg, if you said:
myCar.make you will receive a error saying:
'make' is inaccessible due to 'private' protection level"

2 Likes

Hi Chris,

That makes more sense now - Thank you for your help.

Here is what I have coded - however, it did not returned in the console. What did i do wrong? Could you please help?

struct Car {

var make:String = "British"
var model:String = "Ashton Martin"
var year:String = "2021"
var details:String {
    year + make + model
}
func getDetails() -> String {
    return details
}

}

Below the struct definition (the last closing brace) add the two lines:

let myCar = Car()
print(myCar.getDetails())

and then hover your cursor on the next line number below the print statement and tap on the play button.

If you are not seeing the Console output, then press Shift + Command + C

1 Like

Hi Chris,

This NOW make total sense to me!!! Thank you so much for your help - I greatly appreciate it!! :slight_smile:

1 Like

As an aside, you can format your output from getDetails by doing this:

return "\(year) \(make) \(model)"

What that does is use string interpolation by embedding the variables in a \ () so that you can have spaces between the properties when printed. The other way to do it is like this:

return year + " " + make + " " + model

which achieves exactly the same result but using string concatenation.

2 Likes

Hi Chris,

This is interesting - I like the interpolation alot better. Its less messy and looks alot cleaner!!!

Thank you for opening my eyes to different style of incorporating techniques - :heart: IT!!! :slight_smile:

1 Like