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. I got a little bit confused about this course and Adding Instance Methods

This is what teacher typed. I don't understand why the 'let coordinatePoint = Point(x: 2, y: 2)' is working. Does this mean I was assign value to 'xCoord and yCoord'? struct Point { let x: Int let y: Int

func surroundingPoints(withRange range: Int = 1) -> [Point] {
    var results: [Point] = []

    for xCoord in (x-range)...(x+range) {
        for yCoord in (y-range)...(y+range) {
           let coordinatePoint = Point(x: xCoord, y: yCoord)
            results.append(coordinatePoint)
        }
    }
    return results
}

}

let coordinatePoint = Point(x: 2, y: 2) coordinatePoint.surroundingPoints()

can you help me with the test 'Adding Instance Methods'

struct Person { let firstName: String let lastName: String

func getFullName() -> String {


   let aPerson = Person(firstName: "Peter", lastName: "Parker")

   let fullName = aPerson
    return "\(firstName ) \(lastName)"
}

}

Is this right?

2 Answers

Sure. You don't need to change the function. The editor just wants you to call the function on a Person object, viz., the aPerson object you created in Task 1. I'm showing the whole thing here so you can see how it all fits together. But again, no need to change the function. Just add the last line to call it.

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Peter", lastName: "Parker")
print(aPerson.getFullName())  //prints Peter Parker

let fullName = aPerson.getFullName()

Thank you so much.

Your first question is "I don't understand why:

 let coordinatePoint = Point(x: 2, y: 2)

is working."

What this line of code is doing is assigning a Point object whose x and y coordinates are each 2, to a constant named coordinatePoint.

Then, in the next statement:

results.append(coordinatePoint)

the Point object is appended to the results array, which was declared as an array of Point objects.

If I have misunderstood your question please let me know.

Your second question. Is this correct?

struct Person { let firstName: String let lastName: String

func getFullName() -> String {


   let aPerson = Person(firstName: "Peter", lastName: "Parker")

   let fullName = aPerson
    return "\(firstName ) \(lastName)"
}
}

No, not quite. You didn't give a link to the challenge or the video, so I can only guess. But you need to create the struct and the function first and then use them, and the lines of code using them must be outside the struct, not inside. Something like this:

struct Person {
    let firstName: String
    let lastName: String

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

let aPerson = Person(firstName: "Peter", lastName: "Parker")
print(aPerson.getFullName())  //prints Peter Parker

Thank you. I think I get the first question. Here is the link of the challenge https://teamtreehouse.com/library/objectoriented-swift-20/complex-data-structures/adding-instance-methods Could you help me with the 2/2 ?

I have typed this. Please tell me where is going wrong.

struct Person { let firstName: String let lastName: String

func getFullName() -> [Person] {
    var result: [Person] = []
    result.append(aPerson)
    return result
}

}

let aPerson = Person(firstName: "Peter", lastName: "Parker") aPerson.getFullName()