Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Enums and Structs Structs and their Methods Struct Methods

Following example is leading to errors and I need another set of eyes.

errors at init, return and var lines

Here is the code.

struct Contact {
    let firstName: String
    let lastName: String
    var type: String

    }

    init(fName: String, lName: String){
        self.firstName = fname
        self.lastName = lname
        self.type = "Friend"
    }

    func fullName() -> String {
        return "\(self.firsName) \(self.lastName)"
}

var person = Contact(fName: "Todd", lName: "Deluca")
person.fullName()

4 Answers

Stone Preston
Stone Preston
42,016 Points

you closed the struct prematurely. the init and fullName methods belong inside the struct. remove the } after the type variable. also amit created firstName and lastName as vars, not constants

struct Contact {
    //make these variables
    var firstName: String
    var lastName: String
    var type: String

    //remove the } that was here

    init(fName: String, lName: String){
        self.firstName = fname
        self.lastName = lname
        self.type = "Friend"
    }

    func fullName() -> String {
        return "\(self.firsName) \(self.lastName)"
}

var person = Contact(fName: "Todd", lName: "Deluca")
person.fullName()
Stone Preston
Stone Preston
42,016 Points

thats because you did not capitalize the n in name. you have fname and lname but in the parameters they are called fName and lName

try:

init(fName: String, lName: String){
    self.firstName = fName
    self.lastName = lName
    self.type = "Friend"
}

I followed your instructions and now am getting errors in the init method; on these lines self.firstname =fname and self.lastName = lname

import UIKit

struct Contact { var firstName: String var lastName: String var type: String

init(fName: String, lName: String){
    self.firstName = fname
    self.lastName = lname
    self.type = "Friend"
}

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

} }

var person = Contact(fName: "Todd", lName: "Deluca") person.fullName()

person.firstName = "Tom" person.lastName = "Young" person.fullName()

Thanks Stone. These are probably really basic silly questions but I am new to programming. I appreciate your help. Todd

Stone Preston
Stone Preston
42,016 Points

no problem. I make those kinds of mistakes all the time. syntax errors are easy to make and hard to catch sometimes