Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift 2.0 Complex Data Structures Adding Instance Methods

Paul M
Paul M
16,370 Points

Object-Oriented Swift : Challenge Task 2 of 2 Help

I am on Challenge Task 2 of 2, 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 fullName. I have no idea what to do here, can someone help me?

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

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

2 Answers

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

You're doing great! You've got it all set up properly to make your Person. Now you just need to make them! :smiley:

We do this outside the class definition. Take a look at my solution:

let aPerson = Person(firstName: "Jane", lastName: "Doe")
let fullName = aPerson.getFullName()

So we have a new instance of Person and we're assigning them into the constant aPerson. Then we make a new constant named fullName and assign into it the result of running the getFullName method on this person.

Hope this helps! :sparkles:

Paul M
Paul M
16,370 Points

Thanks! I was unsure that you had to call the function

I tried this exact solution before finding help

It won't pass and says, " the value being passed assigned to fullName must be the result of calling the instance method getFullName()"

My code runs perfect on playground

Any clues?

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

Michael Beers I'm not sure what to tell you. If I take this code and copy/paste it into the challenge... it passes both steps:

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Jane", lastName: "Doe")
let fullName = aPerson.getFullName()

Hope you get it working! :sparkles:

Thanks Jennifer. I got it working! I had String in parentheses after the return arrow. It worked fine in playground. I removed the parentheses and it passed.