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 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