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

Roop P
PLUS
Roop P
Courses Plus Student 6,937 Points

structs.swift

//This Code is correct, but can't move forward. It happens sometimes as their (TTH's) site is looking for specific a process (or syntax). If anyone can tell me the specific way TTH wants this written- that will be nice! Thanks...

struct Person { let firstName: String let lastName: String

func fullName () -> String {
    let  fullName = "\(firstName) \(lastName)"

    return fullName
}

}

let example = Person(firstName: "Annie", lastName:"Jones") example.fullName()

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

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

let example = Person(firstName: "Annie", lastName:"Jones")
example.fullName()
Roop P
Roop P
Courses Plus Student 6,937 Points

//This Code is correct, but can't move forward. It happens sometimes as their (TTH's) site is looking for specific a process (or syntax). If anyone can tell me the specific way TTH wants this written- that will be nice! Thanks...

1 Answer

Matthew Long
Matthew Long
28,407 Points

Your syntax is correct except it wants specific constants. Reread challenge two and it will tell you to create two constants. One named aPerson and one named myFullName:

struct Person {
    let firstName: String
    let lastName: String
    func fullName() -> String {
      return "\(firstName) \(lastName)"
    }
}

let aPerson = Person(firstName: "Billy", lastName:"Boo")
let myFullName = aPerson.fullName()
Roop P
Roop P
Courses Plus Student 6,937 Points

Hey Matt, Thanks for reply! Unfortunately the challenge text does not include that. Here is what is reads

"Given the struct below in the editor, we want to add a method that returns the person’s full name. Declare a method named fullName() that returns a string containing the person’s full name. Note: Make sure to allow for a space between the first and last name"

Though I will make the changes to move-forward. After applying that and moving the next part did mention as you wrote it. Thanks again

Matthew Long
Matthew Long
28,407 Points

Sorry. It appeared that you were already on challenge two because you were creating an instance of Person and using the method you created in challenge one. That is the objective of challenge two:

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 myFullName.

The reason you were having trouble with challenge one is the space between your function name and the parentheses. Remove that space and challenge passes

Roop P
Roop P
Courses Plus Student 6,937 Points

Ah! white space sensitivity with TTH. Thanks, I will keep an eye for that.