Lesson 8 of 14 day challenge

Not sure if I have this right, I feel like I do but looking at it, it doesn’t make sense.

struct Car {
    
    var make: String = "1"
    
    var model: String = "2"
    
    var year: String = "3"
    
    var details:String = ""
    var yearMakeModel:String{
        return "year + make + model"
    }
    
    func getDetails (details:String) -> String{
         
        return details + yearMakeModel
    }
}

Please let me know

Which part about it specifically confuses you? How it’s written?

What I am asking if this is correct. If not, why?
Or is there something missing. I’m super new to coding, so dumbing things down low will help me a lot. Im eager to learn but again, im super new to coding.

Let’s look at the Challenge step by step.

Challenge

Declare a struct called Car

Declare 4 properties called make , model , year and details

Assign String data to the make , model and year properties (use whatever values you want)

Make the details property a computed property that returns a String with the year , make and model . (For example, “1999 Toyota Camry”)

(Hint: You can use the + operator to add strings together. This is also known as concatenation)

Make all 4 properties have the private access level

Finally, declare a method called getDetails which returns the value of the details property.

  1. Declare a struct called Car
struct Car {

}
  1. Declare 4 properties called make , model , year and details
  2. Assign String data to the make , model and year properties (use whatever values you want)
  3. Make the details property a computed property that returns a String with the year , make and model . (For example, “1999 Toyota Camry”)
  4. Make all 4 properties have the private access level
struct Car {
    private let make: String = "Toyota"
    private let model: String = "Yaris"
    private let year: String = "2020"

    private var details: String {
        return "\(year) \(make) \(model)"
    }
}
  1. Finally, declare a method called getDetails which returns the value of the details property.
truct Car {
    private let make: String = "Toyota"
    private let model: String = "Yaris"
    private let year: String = "2020"

    private var details: String {
        return year + " " + make + " " + model
        // you could also say
        //  return "\(year) \(make) \(model)" which uses string interpolation
    }

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

this helps a lot!!!

Also how did you know to set it up that way? I obviously read the question wrong. The way I set it up some far from right. Like I said I’m super new to coding.

You’ll get better with experience. I struggled in the early days of my leaning path so you are not alone.

1 Like

How did you know to use a constant rather than a variable? That’s what I been using. I think were I am getting confused is Qs 4 and 5, especially #5-private access level. I took a break and relooked at this challenge and it still through me off.

@osvaldotl

Hi Osvaldo,

I used a constant because the properties are private which means you can’t change them anyway so make them a let rather than a var.

1 Like

You can also use let by default in case you’re confused @osvaldotl. That’s a small technique I learned by reading the document created by Apple. The same document that sometimes gives me headaches :joy:

1 Like

What does Private mean and when is it used? I don’t recall that being taught in the course or said.

private in the context of any scope in which it is used means that the value cannot be accessed outside that scope.

This may help you understand.
https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

this help but its a lot! thanks!