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

Bob Clanfield
Bob Clanfield
2,372 Points

Does this "not" have an instance method named fullName() in the struct definition?

The error I get is add an instance method named fullName() to the struct definition

structs.swift
struct Person {
    let firstName: String
    let lastName: String
    func fullName ( ) -> String {
        let fullName: String =  (firstName + " " + lastName)
        return fullName
    }}
let person = Person(firstName: "Tom", lastName: "Brown")
let fulname = person.fullName()

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Bob,

Swift is very picky about spacing. More so than other languages.

If you remove the extra spaces in your function definition, you'll be all set. Should look like this:

func functionName() -> ReturnType {
    // function body
}
Bob Clanfield
Bob Clanfield
2,372 Points

Hi Greg, I firmed up any spaces I thought may be the issue but, Im still told to add an instance method named fullName() to the struct definition. Xcode seems to compile a variety of the versions I've tried.

struct Person {
    let firstName: String
    let lastName: String
    func fullName () -> String  {
        let fullName: String =  (firstName + " " + lastName)
        return fullName
    }}
let person = Person(firstName: "Tom", lastName: "Brown")
let fullName = person.fullName()
Greg Kaleka
Greg Kaleka
39,021 Points

Hey Bob,

Your original code in your question had two errant spaces. The code in this comment got rid of one of them (between the parentheses) and added a new errant space (you now have two between String and your opening curly brace). You also have an extra space after your equal sign - there should only be one.

Also note that you don't actually have to do anything with your struct for task 1 (i.e. your last two lines are unnecessary).

Greg Kaleka
Greg Kaleka
39,021 Points

In the interest of being explicit, here are your extra spaces:

struct Person {
    let firstName: String
    let lastName: String
    func fullName_() -> String _{
        let fullName: String = _(firstName + " " + lastName)
        return fullName
    }}
Bob Clanfield
Bob Clanfield
2,372 Points

Yep you were right. Didn't realize extra space = no space. It didn't seem to hiccup or change the compiler in Xcode, so I stared looking everywhere for an answer. Thank Ü for your persistence. ¯_(ツ)_/¯ ॐ

Greg Kaleka
Greg Kaleka
39,021 Points

Yeah I think the challenges use a Swift Linter, which is more strict about formatting stuff. Probably a good habit to get into anyway :blush: