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

What is wrong this time

This was my previous thread https://teamtreehouse.com/community/why-does-this-code-return-that-i-didnt-add-an-instance-method Now, it returns this, even though it clearly does exist.

Bummer! Make sure you're adding an instance method named getFullName() to the struct definition

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func getFullName () -> String{
    return "\(firstName) \(lastName)";
    }
}
let aPerson = Person(firstName : "Yao", lastName : "Liu");
let fullName = aPerson.getFullName;

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! This time it's just a couple of tiny errors. You have an extra space between the name of the function and the parentheses that is causing the challenge to fail. Also on step 2 you will need parentheses to call the method on the instance of the Person object. So you will need:

let fullName = aPerson.getFullName();

And change this:

func getFullName () -> String{

To this:

func getFullName() -> String{

Hope this helps! :sparkles:

Nice one! Didn't know you can't have a space before the parentheses :)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I think you actually can in Swift, but it does cause it to fail in step 1 of the challenge. I'm not on my Mac right this second so I can't confirm :smiley:

thanks. its those little things that people spend hours looking for in bugged code. ¬_¬

You forgot parentheses after aPerson.getFullName :)

Try this instead:

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

let aPerson = Person(firstName : "Yao", lastName : "Liu");

# Over here, I added () after aPerson.getFullname
let fullName = aPerson.getFullName();

Good luck! ~alex

EDIT: Like what Jennifer Nordell said, you must also get rid of the extra space before () -> String

Aananya Vyas
Aananya Vyas
20,157 Points
struct Person {
    let firstName: String
    let lastName: String
//here we need a function called fullName() explicitly acc to the code challenge
///returns full name 
    func fullName() -> String{
        return "\(firstName) \(lastName)"
    }
}

let aPerson = Person(firstName : "anan", lastName : "vy")
//the constant can be named anything coz it is unspecified
let getfullName = aPerson.fullName()```