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

Whats wrong with the code here ?

any help regarding this question?

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

    func fullName() -> (String){

    return "\(firstName) \(lastName)"
    }
}

let name = Person.init(firstName: "Harshal", lastName: "Solanki")

print(name.firstName , name.lastName)

1 Answer

Hi.

So you haven't done exactly what the question asked. here is what I did, and I'll run through it

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
        return firstName + " " + lastName
    }
}

let aPerson = Person(firstName: "Heath", lastName: "Robertson")

let myFullName = aPerson.fullName()

Now first the challenge asks you to assign the instance of person to a constant called 'aPerson', but you assigned it to a constant called 'name', and also called the init method on the Person struct.

let name = Person.init(firstName: "Harshal", lastName: "Solanki") // Your code

To create an instance, you just type out the name of the struct, and the open parentheses and fill in the parameters like so:

let aPerson = Person(firstName: "Heath", lastName: "Robertson") // Correct

For the last part, instead of assigning the the full name to a constant named 'myFullName', you just tried to print out the answer, and you also did not call the fullName method, which is what you are supposed to do.

print(name.firstName , name.lastName) // Your code

Also, your dot notation is around the wrong way. You would normally type firstName.name, but the question does not ask you to do that. It asks you to call the instance method 'fullName', and assign it to a constant:

let myFullName = aPerson.fullName() // Correct

I hope this helps you out :)