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

Kenneth Dubroff
Kenneth Dubroff
10,612 Points

Declare method that returns a string

I do believe I've met the challenge requirements so I probably didn't lol. This compiles in Xcode and prints the name as a string

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

func fullName() -> String {
    let someone = Person(firstName: "John", lastName: "Doe")
    return "\(someone.firstName) \(someone.lastName)"
}
Kenneth Dubroff
Kenneth Dubroff
10,612 Points

Thanks, I did try that, and that's a good tip. This is in the basics track believe it or not.. about 3/4 of the way through

3 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Here is the correct answer; please let me know if you need further explanation! Happy Coding!

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {

        return "\(firstName) \(lastName)"

    }
}

let aPerson = Person(firstName: "Jeff", lastName: "McDivitt")
let myFullName = aPerson.fullName()
Kenneth Dubroff
Kenneth Dubroff
10,612 Points

Thanks Jeff. The answers are identical in functionality. The only differences being that the function is being declared inside of the struct and the constants declared outside of the struct. I'm wondering why this makes a difference when the result is the same

Jeff McDivitt
Jeff McDivitt
23,970 Points

You are correct and your answer will work, it is just not what the challenge is asking for. In coding there are often several ways to complete a task. The main part is that you figured out how to complete it; although for these challenges they are very specific on what is needed to complete the task.

I remember when I wrote my first program in Visual Basic and it was 4000 lines of code. I re-wrote the program and it only took 350 lines of code. As you progress as a programmer you will find out how to simplify things and have reusable code that you can use throughout your program.

Kenneth Dubroff
Kenneth Dubroff
10,612 Points

Thanks Jeff. I did what the challenge asked for... "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"

My method just wouldn't serve much purpose in a real application. It would be helpful if the challenge went into more detail, or best practices (i.e. reusability) were discussed more in the lessons. Heck, even the mention that "your code should be reusable" would go a long way towards me not pulling my hair out lol

Jeff McDivitt
Jeff McDivitt
23,970 Points

Kenneth - I could not even count the number of times that I wanted to pull my hair out programming. It is great that Treehouse provides this forum for individuals. Keep at it and the more you do it this easier it will get for you.