Tag Archives: Programming

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

Learning Swift Programming – Day 1

The Swift Programming LanguageOkay, well technically it’s not Day 1 (but i wan’t to recap my learnings once a day and this is the first post).  I’ve been working through the “The Swift Programming Language” for about a week now. I’m on page 18 of the book and I’ve written ~287 lines of code in the playground. It’s a ton of fun to go through but I find myself not exactly understanding everything.

Specifically this example below.  I understand that is sorting and that reversing the $number will reverse the sort but I have no idea why?  The books says:

“You can refer to parameters by number instead of by name—this approach is especially useful in very short closures. A closure passed as the last argument to a function can appear immediately after the parentheses.” 1

Reading that above, and writing the code below I still don’t understand what $0 and $1 are referring to? They are a stand-in for the variables but what variables exactly? I understand that I must be passing the array of numbers and ordering them based on one being more than or less than the next. I also get in the side bar that to complete the first and second function it took 9 iterations. I’m assuming then that the sort function must sort an array… and that maybe it’s running until the function returns false so until $1 is no longer > than $2. But I’m totally guessing here. Any help understanding this would be appreciated.

let mySort = sort([1,5,3,12,2]) { $1 > $0 } // no idea what this is doing?  I'm assuming it is sorting the array of numbers.
mySort

let theSort = sort([1,5,3,12,2]) { $0 > $1 } // no idea what this is doing?  I'm assuming it is sorting the array of numbers.
theSort

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