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

Jeremy Lyles
Jeremy Lyles
3,886 Points

Does the compiler for this challenge work?

When I copy and pasted the code that I initially wrote in the browser into my Swift playground, the results read that the compiler does not work despite the code working in my playground. Is there something wrong with the browser compiler, or am I missing something?

structs.swift
struct Person {
    let firstName: String = "Chris P."
    let lastName: String = "Bacon"

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

let aPerson = Person()
let myFullName = aPerson.fullName()

1 Answer

Magnus Hållberg
Magnus Hållberg
17,232 Points

Yes it works. Its just that you are supposed to use the struct "built in" initializer, not to give the properties them selves values. In this case the aPerson constant should look like this: let aPerson = Person(firstName: "Chris P.", lastName: "Bacon"). Good luck

Jeremy Lyles
Jeremy Lyles
3,886 Points

Thank you for the answer! this actually helped a few things click in my head about the core differences between structs and classes in Swift.