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

Sebastian Fernandez
Sebastian Fernandez
2,413 Points

what is wrong?

i don't know what is wrong.. pls help

structs.swift
struct Person {
    let firstName: String
    let lastName: String
}
 let fullName = Person(firstName: "Sebastian ", lastName: "Fernandez")

3 Answers

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

Hi there! I'm unsure if you're working on the first step or the second step. If you're working on the second, then something drastic has changed in your code. The first step requires you to create a method on the struct named fullName. This method will return a string that contains the first name of the person, the last name of the person, and a space in between. Your code as it now contains no functions whatsoever.

It's not until the second step where you are asked to create an instance of the struct and this is to be assigned to the constant aPerson not fullName.

I think you can get it with these hints, but let me know if you're still stuck! :sparkles:

Sebastian Fernandez
Sebastian Fernandez
2,413 Points

Hi Jennifer, thanks for the answering but I still stuck in the second step can you tell me more hints pls ?

struct Person { let firstName: String let lastName: String func fullName() -> Person { let myFullName = (firstName = "Sebastian",lastName ="Fernandez") return myFullName } }

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

Hi again! So you've gotten a function now, but it should return a string. You're trying to return a Person. Also, we want the string returned to be a combination of the firstName and the lastName no matter what two names a person has. You are attempting to hard-code yours. You say that you are stuck on Step2 but your code does not pass Step 1.

Step 2 will simply require one additional line of code outside of the solution to Step 1.

Sebastian Fernandez
Sebastian Fernandez
2,413 Points

Hi Jennifer, sorry because I don't understand the question (I feel really stupid), but the code is doesn't work...

Can you help me for the last time ??

This is the code:

struct Person { let firstName: String let lastName: String func fullName(aPerson: String) -> String { let aPerson = "(firstName) (lastName)" return aPerson } } let myFullName = Person(firstName: "Sebastian", lastName: "Fernandez")

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

Hi there, Sebastian! Something is happening to your code between step1 and step2 because the code you have would not pass step 1 and that was true when I left my first answer. I'll show you my solution and walk you through it:

struct Person {  //declare a struct named Person
  let firstName: String   // Person has a firstName
  let lastName: String   // Person has a lastName

  func fullName() -> String {  // function to return the person's full name
    return "\(firstName) \(lastName)"  //return a string with the first ans last name interpolated
  } 
}

let aPerson = Person(firstName: "Sebastian", lastName: "Fernandez")  //create a person and assign it to the constant aPerson
let myFullName = aPerson.fullName()  // get the person's full name and assign it to myFullName

I feel like there is some misinterpretation of the instructions going on. They clearly say to make a person and to assign it to a constant aPerson. Then they say to get that person's full name and assign it to a constant myFullName.

Hope this helps! :sparkles:

Sebastian Fernandez
Sebastian Fernandez
2,413 Points

Thanks Jennifer, you are awesome!!