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

Dan Hillman
Dan Hillman
4,394 Points

Assigning Instance Method to Constant. What am I doing wrong?

The constant fullName is being assigned the result of method getFullName of type Person. The constant fullName is supposed to be equal to a string with the value "(firstName) (lastName)". When this code is run on Xcode, everything runs correctly and the values are correct. Is there something I am missing?

Click here for a screenshot with the "Bummer" message

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

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

let aPerson = Person(firstName: "Joe", lastName: "Smith")
let fullName = aPerson.getFullName()
Daniel Aunspach
Daniel Aunspach
942 Points

I have the same issue and my return was in parenthesis. return("(firstName) (lastName)") and it passed the first part of the challenge.

Aside from this, I have the exact same code as the OP.

I've spent over an hour trying to coax the editor to allow me to pass, even though Swift playground says it's fine and returns the proper value and assigns it to the constant fullName.

2 Answers

I provided my answer in the bottom that worked. The only difference I see is the Parenthesis with String. Try removing it and see if it'll work.

struct Person {
  let firstName: String
  let lastName: String

  func getFullName() -> String {
    return "\(firstName) \(lastName)"
  }
}
let aPerson = Person(firstName: "Steven", lastName: "O")
let fullName = aPerson.getFullName()
Daniel Aunspach
Daniel Aunspach
942 Points

Yes, you are correct. Thanks for the help!

IMHO, if Xcode doesn't complain, the editor shouldn't, either. In Xcode, if there's a syntax error, it will at least provide a clue where the error occurs.

Further, the editor, in my opinion, should not allow (String) to pass the FIRST challenge and then balk at the SECOND challenge that builds on the premise that what you did in the first was correct. If the editor had not allowed me to pass the first challenge, I'd have found the problem.

Lastly, the editor's error does not provide an accurate report as to the actual error (pointing to syntax in the SECOND block of code). Therefore, I submit that it's a bug in the system, but I think Team Treehouse disagrees.

Steven, thank you again for the help. It is much appreciated!

Dan Hillman
Dan Hillman
4,394 Points

That's the answer! Apparently, when the return type is specified as "(String)" instead of just "String", it returns a tuple with a string instead of just a string. Pasan emailed me and said he's adding a better error message that takes care of this.

Reed Carson
Reed Carson
8,306 Points

I think this is just a quirk of the editor they use. Try putting the return statement within the method of your struct in parentheses. Code is fine, editor just weird.