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 2.0 Complex Data Structures Adding Instance Methods

Mark Carrel
Mark Carrel
9,873 Points

Swift code won't compile in treehouse (works fine in xcode), but there are no compiler errors in preview.

This code won't compile and run in treehouse (but works fine in Xcode). Worse, no compiler errors show up in the preview window, so I don't know what the treehouse compiler is having trouble with. It does seem to have something to do with the last line of code since the error goes away when I remove it.

structs.swift
struct Person {
    let firstName: String = "Bob"
    let lastName: String = "Roberts"

     func getFullName() -> String {

        let theName = firstName + " " + lastName
        return theName
    }

}

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

4 Answers

David Papandrew
David Papandrew
8,386 Points

For Step 2 you need to make sure you are passing firstName and lastName arguments when creating the Person instance.

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "John", lastName: "Doe")
let fullName = aPerson.getFullName()
Tassia Castro
seal-mask
.a{fill-rule:evenodd;}techdegree
Tassia Castro
iOS Development Techdegree Student 9,170 Points

Hey Mark,

Your code is correct indeed, the problem is just that Treehouse is not asking you to assign any value to firstName or lastName and you should also remove the instantiation of person as it is not required for this task.

struct Person {
    let firstName: String 
    let lastName: String

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

}
Mark Carrel
Mark Carrel
9,873 Points

Thanks Tassia! It looks like you are referring to challenge task 1. My question was about challenge task 2, which does ask you to instantiate Person and call the instance method. Sorry for the confusion.

Mark Carrel
Mark Carrel
9,873 Points

Thanks, David, that fixed the problem!