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

Helper method lesson and scope

Hello,

I think I'm having a basic understanding issue with the idea of scope as it applies in this particular lesson.

I'm currently working on the Helper Method lesson, the 5th step of the 'Classes in Swift' part of Object-oriented Swift 3 course.

In this lesson, we are adding a helper method to a class named Tower and will use this function to determine if the enemy is in range for firing.

In the programming for this method, we use the line below:

let availablePositions = position.points(inRange: range)

But the points method called above was created in a different class. I would think that would make that function out of scope and unavailable to use. But I guess I'm wrong.

Can anyone give me a hint on understanding this?

3 Answers

Hey Mary Kenkel,

Unfortunately I'm unfamiliar with this lesson, however, I feel like the functionality you are describing is referred to as "inheritance". When a class subclasses another class, it automatically inherits the superclasses methods and properties.

For example:

class Vehicle {

    var numberOfWheels: Int
    var numberOfDoors: Int

    init(wheels: Int, doors: Int) {
        numberOfWheels = wheels
        numberOfDoors = doors
    }

    func move() {
        print("Start moving")
    }
}

class Car: Vehicle {}

let car = Car(wheels: 4, doors: 2)
car.move()

// Since Car subclasses Vehicle,
// it has all the methods, properties, and initializers that Vehicle has.

Hopefully this clears somethings up.

Good Luck

Hi Steven,

Thank you so much for the response!

I don't think we're using a subclass. I don't think we've learned about those yet.

Here's the coding. You can see that a method named points is defined in the struct named Point. Then that same method is used in the Tower class in the function named isInRange (in the programming line: let availablePositions = position.points(inRange: range))

I just didn't think that you could do that with the whole 'in-scope' concept.

Anyway, if you have any thoughts, I'd be very appreciative!

struct Point {
    let x: Int
    let y: Int

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
    }

    /// Returns the surrounding points in range of
    /// the current one
    func points(inRange range: Int = 1) -> [Point] {
        var results = [Point]()

        let lowerBoundOfXRange = x - range
        let upperBoundOfXRange = x + range

        let lowerBoundOfYRange = y - range
        let upperBoundOfYRange = y + range

        for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
            for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
                let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
                results.append(coordinatePoint)
            }
        }

        return results
    }
}

//let coordinatePoint = Point(x: 0, y: 0)
//
//coordinatePoint.x
//coordinatePoint.points()


class Enemy {
    var life: Int = 2
    let position: Point

    init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
    }

    func decreaseLife(by factor: Int) {
        life -= factor
    }
}

class Tower {
    let position: Point
    var range: Int = 1
    var strength: Int = 1

    init(x: Int, y: Int) {
        self.position = Point(x: x, y: y)
    }

    func fire(at enemy: Enemy) {
        if isInRange(of: enemy) {
            enemy.decreaseLife(by: strength)
            print("Gotcha")
        } else {
            print("Darn! Out of range!")
        }
    }

    func isInRange(of enemy: Enemy) -> Bool {
        let availablePositions = position.points(inRange: range)

        for point in availablePositions {
            if point.x == enemy.position.x && point.y == enemy.position.y {
                return true
            }
        }

        return false
    }
}


let tower = Tower(x: 0, y: 0)
let enemy = Enemy(x: 1, y: 1)

tower.fire(at: enemy)

Hey Mary Kenkel,

You're correct, we're not subclassing here, but keep this post in mind for a future lesson. What is happening, however, is the Tower class has a property named position of type Point. We can then access the properties or methods of that property by using dot syntax.

Hope this helps

Oh! I didn't know that.

Thank you so much Steven! That really helps! I'm going to say this page to my notes.

Thank you again...