The Swift Programming Language – Day 2 – Classes

Oof, starting out the day kind of rough yesterday. I’m working through the first two paragraphs of the book’s section on introduction to classes and I’m a bit stumped. In general I understand object oriented programming, inheritence, polymorphism, etc… Having said that I sometimes get caught upon the syntax and the issue I’m having today doesn’t even really have to do with classes I don’t think. Right in the beginning of the intro to classes there was the following experiment:

EXPERIMENT
“Add a constant property with let, and add another method that takes an argument.” 1

I understand what ‘creating a constant property’ is and know how to do this using let:

let shapeColor = "Green" 

What I don’t understand is the “add another method that takes an argument” part. An argument is just a variable something that you pass into a function. I assume it just wants me to create a function (is that synonamous with method?) that takes an argument… which I’m assuming it’s trying to get me to use the constant that I just created (shapeColor). I don’t really understand this in context of the class I just created? What am I missing here? Here’s an exerpt of the code I just wrote from the example in the book:

class Shape {
    var numberOfSides = 0
    let shapeColor = "Green"
    func simpleDescription() -> String {
        return "A Shape with (numberOfSides) number of sides."
    }
}

After doing a bit of reading on StackOverflow I think I might have a feel for it now. It seems like a method is a function that you use to get at a property contained in a class. In the example below. I’m just calling the method and passing it a color which then returns a string that uses the color I passed into it. I think i was a bit confused because the book was calling it a method but the sytax still refers to it as a function (‘func’).

class Shape {
    var numberOfSides = 0
    let shapeColor = "Red"
    func simpleDescription() -> String {
        return "A Shape with (numberOfSides) number of sides."
    }

    func getColor(color:String) -> String {
        return "My shape is the (color) color - which is very nice."
    }

}

var shape = Shape()
shape.getColor("blue")

  1. ”Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
    “`