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

Ken Gerlinger
Ken Gerlinger
2,898 Points

How to call the instance method and assign the full name to a constant

Hello, I tried to manage this challenge, but something is wrong.

Please explain to me where my mistakes are.

Cheers,

Ken

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func fullName()-> String {
    var Name = "\(firstName) \(lastName)"
    return Name 
    } 
}

let aPerson = Person(firstName: "Ken", lastName: "Gerlinger")

let myFullName = aPerson

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Ken,

You are doing good.. In fact, all you needed was just one small thing to pass the challenge.

// YOUR CODE

struct Person {
    let firstName: String
    let lastName: String
    func fullName()-> String {
    var Name = "\(firstName) \(lastName)" // properties should have lower case
    return Name 
    } 
}

let aPerson = Person(firstName: "Ken", lastName: "Gerlinger")

let myFullName = aPerson //<-- Missing the instance method call

// You are not calling the method from the instance.
// The method is the one that creates the full name.

Things we can do to improve the code

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
    return "\(firstName) \(lastName)" // You can call these properties directly in the return
    }
}

let aPerson = Person(firstName: "Ken", lastName: "Gerlinger")
let myFullName = aPerson.fullName() // Calling the instance method

You did well, just small little details that you will learn to make your code look and function better as you practice more.

Good luck.

Ken Gerlinger
Ken Gerlinger
2,898 Points

Jhoan thank you for your kind advice !