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 2.0 Complex Data Structures Adding Instance Methods

Aditya Gawade
Aditya Gawade
1,182 Points

i was only able to solve the upper part due to previous help. i have no idea what is going and why :( can u xplain pls?

Let's use the struct to create an instance of Person and assign it to a constant named aPerson. Assign any values you want to the first and last name properties.

Once you have an instance, call the instance method and assign the full name to a constant named fullName.

structs.swift
struct Person {
    let firstName: String
    let lastName: String

    func getFullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

let aPerson = Person(firstName: "Aditya", lastName: "Gawade") 
let fullName = Person.getFullName

2 Answers

Let me try to explain... The struct you have contains two data properties firstName and lastName and both are String It has also a function getFullName that returns String. This function is using a concept in swift called string interpolation, which means you can amend a variable inside a text , you do this by mentioning the variable name inside (). so when you have something like

"my name is \(nameVariable)"  // this will return a text with the value of nameVariable  

The fucntion getfullName is just doing this interpolation for firstName and lastName and returns a string that contains firstName and lastName content with a space inbetween.

The last two lines of your code is creating an instance of the struct by passing values to the structintializer. The last line is calling the getFullName on the instance you created. however it is not written correctly.. it should be

let fullName = aPerson.getFullName()
Aditya Gawade
Aditya Gawade
1,182 Points

thank u very much. that helped a lot. I'm just a little foggy on the working of the last n line. what is its working?