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

mahi
mahi
7,137 Points

I just can't figure this out!!!

I am not able to get it. i've been trying this from last one hour but am able to crack this.

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

    func getFullName() -> String{
        return "\(firstName) \(lastName)"
    }
}
mahi
mahi
7,137 Points

The step after this challenge which asks to create an instance method getFullName() in the struct Person, am not able to crack that.

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Are you having trouble with part 2 of this challenge?

part 2 requires you to do 2 things, and it goes as follow.

struct Person {
    let firstName: String
    let lastName: String

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

// step 1
// 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.
let aPerson = Person(firstName:"John", lastName:"Doe")

// step 2
// call the instance method and assign the full name to a constant named fullName.
let fullName = aPerson.getFullName()

Let me know if you have question about the code.

mahi
mahi
7,137 Points

thanks a lot!!

Paul Brazell
Paul Brazell
14,371 Points
struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "A", lastName: "Person")
let fullName = aPerson.getFullName()
mahi
mahi
7,137 Points

Thanks a lot!!!