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

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

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

I'm sorry. I give up. I'm not sure if I can ask everytime I can't pass the challenge. But I've been trying to solve it whole day. I went again through last video about instance methods. And no success. Still the same bummer. So what can I do? So maybe could you help? I'm adding function getFullName with return type "Person" like my "struct". So I think it is method. So why there is still bummer that I need to be sure that I'm adding instance method named getFullName. I've also given instance "let fullName". One of many my codes is:

struct Person {
    let firstName: String
    let lastName: String
    func getFullName(a: String, b: String) -> Person {
        let a = "\(firstName) "
        let b = lastName
        let fullName: Person = Person(firstName: a, lastName: b)
        return fullName
    }
}

3 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

Perhaps you are overthinking. It is very important to follow the directions of the challenge. You should do them step by step. Let's break it down.

Challenge Task 1 of 2

  1. Given the struct below in the editor, we want to add a method that returns the person’s full name.
  2. Declare a method named getFullName() that returns a string containing the person’s full name.
  3. Note: Make sure to allow for a space between the first and last name
// Step 1.

/*
 We know that they want us to create a function. 
"Method" is the name of a function that is inside a class or struct.
*/

// Step 2. 

// Declaring a method

struct Person {
    let firstName: String
    let lastName: String

// A method that returns a String, and has no parameters.
func getFullName() -> String {
 return "\(firstName ) \(lastName)"
}

/* 
It is important to know that in the description does not 
say that we have to give parameters to this method.
*/
}

// Step 3

/*
We provided a space in "\(firstName )". 
*/

Challenge Task 2 of 2

  1. Let's use the struct to create an instance of Person and assign it to a constant named aPerson.
  2. Assign any values you want to the first and last name properties.
  3. Once you have an instance, call the instance method and assign the full name to a constant named fullName.
struct Person {
    let firstName: String
    let lastName: String

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

// Step 1 & 2

// For step 2 we are assigning "John" & "Smith"

// Here we are declaring a constant and assigning an instance of Person to it. 
let aPerson = Person(firstName: "John", lastName: "Smith")

// aPerson is now an instance of Person.

// Step 3

let fullName = aPerson.getFullName()

by calling aPerson.getFullName() we get in return a String of the full name as implemented in the getFullName function. That value is then assign to a constant named "fullName".

I hope this answer helps you a bit more. I know it can get frustrating sometimes, but remember the more you practice the more you will understand it. At some point when I was learning, I was in the same situation as you, but now it makes a lot more sense to me. So keep up the good work and good luck.

If you find this answer helpful, then helps us help others by selecting it as best answer. This way other students can also learn from it.

Good luck

Thanks for the review! I cannot understand why Treehouse cannot give more exercises so we can get a better grasp on the concepts. Obviously once you see the code it's easy to understand what's going on, but they do not do enough exercises and it's frustrating.

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

Hello Jhoan. Thank you very much for your help. Now it works perfect and - what is more important - I understand it. Thanks again. Best Regards

Lewis Morgans
Lewis Morgans
3,167 Points
struct Person {
    let firstName: String
    let lastName: String

    func fullName (firstName: String, lastName: String) -> String {

   // return self.firstName + lastName
    let fullName = ("\(firstName) \(lastName)")
    return fullName

    }
}
//Person.init(firstName: "Jax", lastName: "Morgans")
let fullName  = Person(firstName: "Jax", lastName: "Morgans")
fullName.Person(firstName: "Jax", lastName: "Morgans")

Ok I give up. The code works. I don't see what the problem is. I even tried removing the parameters in the function () and it still gives me the same error. I removed everything outside the struct too and still same error :|