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 

Leave a Reply

Your email address will not be published. Required fields are marked *