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

Rohan Prashanth
Rohan Prashanth
1,205 Points

Can someone help me figure out why my code for the code challenge isn't working?

The hint given is that I should make sure I am adding the instance in the struct definition

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


    init(firstName: String lastName:: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

        func fullName(firstName: String, lastName: String) -> Person  {
                var result = Person(firstName, lastName)
      }
  return result
}

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points

Hey,

a struct doesn't need a custom initializer. Also the initializer definition has 2 errors:

  1. Between first name and last name parameters needs to be a comma.
  2. You used two colons behind the lastName parameter

the right way it would look like this

init(firstName: String, lastName: String) {

Next error, you are returning the result outside of the function.

Next error: The function should return a String and not an instance of Person

Next error: The string should be the first name + a space + the last name

Hope that helps, please don't forget to select this as a best answer if it solved your problem.