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

I don't understand how can i create a method fullname() which returns a string of struct type.

In the last video you have made a method which returns an array of string which are Point types. But i don't understand how i can return a string that is Person type. Please explain ... I am stuck in here since last 2 days.

Qiestion: Given the struct below in the editor, we want to add a method that returns the personโ€™s full name. Declare a method named fullName() that returns a string containing the personโ€™s full name. Note: Make sure to allow for a space between the first and last name

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

2 Answers

Shade Wilson
Shade Wilson
9,002 Points

String and Person are both types. In Swift, objects can only have one type, so you can't return something that's both a Person and a String. In this challenge, they're just asking you to return a String.

I'll help you get started:

struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
      [insert your code here]
  }
}

`

Jose Esplana
Jose Esplana
12,503 Points
struct Person {
    let firstName: String
    let lastName: String

    func fullName() -> String {
        let myFullName = "\(firstName) \(lastName)"
        return myFullName
    }
}

let aPerson = Person(firstName: "Jose", lastName: "Esplana")
let myFullName =  aPerson.fullName()