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

rico petrini
rico petrini
522 Points

i am very confused on how to declare a method could someone please help me by giving me an example or a hint please.

i am very confused on how to declare a method could someone please help me by giving me an example or a hint please.

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

2 Answers

There is not much to this function, you can take care of everything on the return line.

struct Home {
    let city: String
    let state: String
    let country: String

    func fullName() -> String {
        return "\(self.city), \(self.state) \(self.country)"
    }
}
István Halász
István Halász
2,896 Points

Hi, you need to use the func keyword to create an instance method, so like you are declaring a function. in your case you need a function that takes no parameters and returns a string, the full name. The function declaration looks like this:

func fullName() -> String {

}

(But you need a return line too!)