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

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

honestly have no clue how to do this.

how do I do this.

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

    func fullName() -> String {
         return firstName + " " + lastName
    } 
}

let aPerson = fullname()

2 Answers

The issue you're having is that you're trying to access the Person struct's fullName() method without first creating an instance of the struct. Once you do that, you would then assign the instance's method call of fullName() to a constant called myFullName.

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "", lastName: "")
let myFullName = aPerson.fullName()
james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

the first part of task 2 is to create an instance of Person and assign it to aPerson. so you have the let aPerson = ..., but you're not ready to call the fullName function yet. to instantiate a Person, you call it by its class or struct name and give it the arguments its constructor is looking for, here that is firstname and lastname. the syntax is Person(firstname = "name", lastname = "name"). when that line executes it will create a Person object with the given names, and it will be stored in your variable. NOW you can call your instance methods on it. so the second part, assign to the constant myFullName your Person with the fullname method called on it using the dot notation.