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

Bob Clanfield
Bob Clanfield
2,372 Points

Works but wrong, wrong, wrong

this is one of 20 or so variations that I've tried struct Person { let firstName: String let lastName: String func fullName ( ) -> String { let fullName: String = (firstName + " " + lastName) return fullName }} let person = Person.init(firstName: "Tom", lastName: "Brown") let name = person.fullName()

structs.swift
struct Person {
    let firstName: String
    let lastName: String
   func fullName ( ) -> String {
        let fullName: String =  (firstName + " " + lastName)
        return fullName
    }}
let person = Person.init(firstName: "Tom", lastName: "Brown")
let name = person.fullName()

2 Answers

Alexandre Attar
Alexandre Attar
10,354 Points

You were almost there!

You did not have to have to use an init method there. In a sens, it does work since your are providing initial values to your firstName and lastName. However, in this case it was not asked an init method to be used therefore why it could have raised an error.

Also, you forgot to put an "a" in front of your "aPerson" constant. Which could have been the other source of error.

Here is my solution to this code challenge:

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

}

let aPerson = Person(firstName: "John", lastName: "Doe")

let myFullName = aPerson.fullName()

I hope it helps! :)

Bob Clanfield
Bob Clanfield
2,372 Points

Hey Alex Thank Ü but that does not seem to be what they are asking for. The error I get is that I need an instance method named fullName() to the struct definition. It seems like we have that though....

Alexandre Attar
Alexandre Attar
10,354 Points

Hmm strange I just tested my code on the challenge it works :/

Make sure you don't have extra spaces between your parentheses and that your constants are properly named.