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

Nicholas Moore
Nicholas Moore
1,117 Points

I don't understand what a "struct" is or does

I watched the video twice and don't understand what the "struct" does. And what are objects and instances? Instance methods?

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

    func fullName(first: String, last: String) -> (String) {

    var name = Person("first ", "last")

    return name

    }


}
Kirby Ziada
Kirby Ziada
9,886 Points

A 'struct' is the building block to your code, use a 'struct' over a 'class' if it's more simplistic. The reason being a 'class' can inherit from other 'classes', a 'struct' can't.

• Instances are basically variables/constants in your struct
• Instance methods are functions within the struct

You can find more information here on Methods (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html) Structs and Classes (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html)

Nicholas Moore
Nicholas Moore
1,117 Points

Ok, so then could you tell me what is wrong with my code in the challenge?

2 Answers

struct Person {
    let firstName: String
    let lastName: String

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

let someName = Person(firstName: "FirstName", lastName: "LastName")
someName.fullName()

Hello Nicholas,you can try something like this..

struct Person { let firstName: String let lastName: String

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

}

let someName = Person(firstName: "FirstName", lastName: "LastName") someName.fullName()

Struct is an object, and unlike classes you don't need to write it s init method,structs has memberwise initializer so you can right away assign instance of struct in this case to constant someName and just call method fullName on that instance(fullName is instance method and you can get full name by returning string with string interpolation)

Hope this helps..