I did a little presentation for Cocoaheads Shanghai on Swift on June 12th
Swift was released last monday at WWDC
It’s really new and there is a lot more to be learned than what has been presented at WWDC
If you have watched all the WWDC video on Swift and already read the Language book then you may have a head start on me. If you’ve gotten in to writing code you are also doing better than me
I’m trying to make this presentation similar to the lectures I received in College about new languages, I took a programming languages course and had to break down nearly every popular language since ADA
To Apple Says Swift is these 4 things, and after reading all the published goodies out there I’m going to back up their claim
We as Cocoaheads need to know how its going to be useful in our current projects and Apple has built with with Cocoa development in mind. Almost purely Cocoa development in mind.
I’m going to talk about LLVM a little later
But incase you forgot a optimizer takes your readable code and puts in better algorithms on your behalf and Autovectorizer helps you with multiple threading, parallel computing.
Its complicated stuff but just remember that it is there to make Swift fast while also being readable and concise
Swift is really all about Cocoa
For right now I expect that developer will only work in Swift for client side Graphical user interface based applications
Its not a scripting language, it doesn’t have an interpreter, it isn’t going to be replacing PHP or Ruby, There are very few tools in the language or the Cocoa libraries to get this on the server.
But you can replace scripts with Swift. Since its readable its a good first language. We will get into that more later.
So why did Apple bother with creating Swift? Objective-C is currently the number 3 language in the world according to Tiobe a tracker of such things.
I think its understandable when you look at the stack of languages Apple was working with
So lets start off with C
No Objects, highly functional, Pointers everywhere, very close to the metal, you can throw in assembly whenever you want to
Then Objective- C which is a superscript of C with Objects. Its getting better but still has all the problems of C like pointers. its also Old, not as old as C but still old. This language is all Apple as well and was brought into the company when Steve Jobs came back to the company after Apple bought NextStep.
C++ isn’t used by many of us but its important for gaming and lets face it, C++ is a great language. It was my preference in College. We know this isn’t Apple’s go to language, Obj-C is, just a language they support because some very important industries rely on C++
Now there is Swift and no pointers and a whole lot more.
All of this is being compiled in Xcode with the LLVM compiler.
This work in the compiler has a lot to do with the conception of Swift
C family with the parens
java with that dot syntax
I’ve heard Rust, and I’ve heard Go! but I don’t know these well enough to explain
So Back to Apple’s baby LLVM
Sprang out as optimizations of the GCC compiler, thats the GNU C compiler project.
GCC was a great compiler and everyone was using it, things looked good for this open software.
LLVM is about modules that make up a compiler, you can pick and choose what and where you want to optimize.
Because of the modules you can perform some tricks at compile time (which when you think about it is also run at someones runtime) and things get compiled faster.
Compiling faster is one thing, but the performance of that compiled code matters as well
LLVM now produces code that is slightly faster than GCC, but that wasn’t true for a while
All of this is the brainchild of Chris Lattner,
As a student he created the LLVM compiler, he stuck with it and got hired by Apple
He has risen in the ranks is now the Director of Developer Tools
He created Swift because of his work the the LLVM compiler the two work very closely together.
He is in charge of your Xcode so when your Xcode 6 Beta crashes this summer feel free to curse his name.
From here on out I’ll be giving you guys some code samples of Swift instead of a history lesson
Heres a quick example of some Swift
From a purely cursary look things look like Objective-C
What I notice are the things it doesn’t have, the things Apple trimmed from Objective-C
notice no Main function
No @ infront of your strings !
Why did Chris make Swift, if You like John Siracusa you’d know pointers Suck and thats why Apple needed this language
Assignments in C do really funny things like returning true and therefore we had some lines of code that wrecked our app and took us hours to find
C is word, and has semicolons everywhere and isn’t natural to read
And then there are overflows which I don’t want to get into but when this happens in your Augmented reality code you’ll hate yourself
Apple said concise, readable code, this isn’t Ruby or Wolfarm Alpha speak while writing but it is pretty good looking as a Cocoa developer.
Types don’t needs to be declared but can be
We keep our prettier colon
Tuples make life easier for readability as well
A major problem with Obj-C was you didn’t have multiple return types
Swift adds this
Think about your hacks!
Functions can be wrapped up and taken on the go in Swift.
Then again maybe we can learn and if you watch the WWDC videos you can create some great algorithms with this syntactic spice. In Advanced Swift using the Memoable function was mind blowing.
Ever wanted to make a category on all those NS Containers like NSArray and NSDictionary
Generics make it happen.
look at that T there for the type, you can also add that T must conform to a protocol so that should help your code clarity as well
This should save a lot twice written code that was in a lot of our Cocoapods
Need your Cocoa to do something unexpected?
Categories was how you did it in Obj-C now you have extensions and Extensions do a lot more, you can put in more places than just classes in Swift
Heres a common CocoaTouch function, app did finish launching with Options!
These two characters are going to be everywhere in your Swift Code when working with Cocoa
Now Look at how you set up the window, the ! checks the property to make sure the window responds to this selector and the question mark checks for a function that should exist on makeKeyVisible.
Using the ! operator to unwrap an optional that has a value of nil results in a runtime error. so you gotta be sure about things
A lot of selectors in Obj-C has a syntax that allows one of the parameters to be nil, this ? mark does the check for it it is nil AND ask if the object passed responds to the selector, a
For when you just don’t care you can tell the compiler that, this means that autovectorizer and code optimizer can make your code run faster
We oftentimes program our own collection objects. If you know how these objects should be described as if in a list then use Subscripts.
This can lead to great weirdness if you are not careful, but also great clarity to you the developer.
Cocoa has so many protocols that we use every day, I’de bet all my view controllers conformed to at least one, its a great was to keep your code generic and modular.
Swift brings the greatness of protocols to more than classes, add it to a struct or even and enumeration!
Look at that toggle on the enumeration!
I didn’t think you could do this in any language, because I’m so use to using functions with parens to do things to my objects
now swift can override an operator to make my code more readable
Also look at the triple plus
your swift classes can be a subclass of objective-c classes.
This first blew my mind but when you think about why Apple wrote Swift then its an obvious feature of the language.
Swift runs in the Objective-C runtime after all
Theres also toll free bridging meaning a swift array is the same as a NSArray, can thats down to the memory!