>> Hello?
Sweet.
Yeah, so my name is Neil Kimmett.
I work for Harry's, we are a razor company.
There are some of our beautiful razors there.
And this is a Swift introduction.
So what is Swift?
You might know it as that language you all know about it as developers, right?
It was introduced in 2015 by Apple to supersede objective-C which is quite shocking.
This was when they first introduced it.
So here, Craig is saying, what would it be like if we had Objective-C without the C and
then a bunch of developers did some murmuring and he says, we did more than that, we have
a new programming language and then whoa!
There's a bunch of super-excited Apple nerds and then he says, it's called Swift, oops,
it's called Swift and it totally rules at.
[laughter] That's actually how they introduced it.
It does totally rule.
So let's dive into some Swift.
We're going to go through a lot of code today, so trap in.
Here's a function calculation.
You have the func keyword, the name of the function is move and we go from start which
is a point to end which is a point and we return a distance.
So two things, there are types everywhere.
Swift is a compiled statically type language because great because if you try to pass the
wrong thing into the wrong function, the compiler won't let you.
Which is fantastic.
The second thing is there's two names for each parameter.
We have an external name and an internal name.
We have from start and to end.
So we'll use start and end within the function and from and to outside the function.
So the idea being that you read at the you read it sort of like a sentence, thing move
from here to there.
Let's look at some more Swift, so here is a list of names.
A list of strings.
And we can do functionally sort of things, using map.
Functions of first-class citizens.
This is a little anonymous function here.
We can map over stuff but really the only interesting part of this code is the appending
is super cool bit.
We have a lot of redundancy here.
We have a lot of repeated types.
Thankfully Swift has this powerful engine so on the first side, the compiler can tell
that on the left-hand side we have just have a list of strings and so this can go away.
And on the second line it can show it's being passed a line of strings so we don't need
this here.
We can also use trailing syntax here to get rid of this bit here and we can use short
argument syntax to get down to this.
Behind the scenes is all of these strong types.
So now we're going to sort of take you on a tour of different kinds of types in Swift
and we're going to build this system that where we ask for a user's date of birth and
we tell them if they're born on a weekend.
So we're going to start with our classes in Swift like most object-oriented language.
A class has properties sore instance variables.
We have var, name and let, date of birth.
What's going on there?
So var means you could mutate it.
Someone could change their name at some point in the future.
Let is a constant which means you can't change the value of it.
You ever couldn't change your date of birth.
That doesn't make any sense.
So it has an initial value and that's the value the whole time.
So we need to give it some initial values.
So we have an initiallizer where we can pass in some values and then we can put in some
functions of values.
So classes are reference types in Swift, so that means as you pass it round your system
and more references are made to it, the reference count of that instance goes up, and then once
all the references are gone away, once bits of the system don't care about it anymore,
the reference count goes to zero and the reference gets counted.
There's also value types in swift.
So reference types of great are for things which has identity like a person, you're not
going to copy a person, that doesn't make sense, but for things which are just more
data-like and you want to be able to pass between threads without like rate conditions
or anything, we can use things like structs, we can implement a date formatter, we're going
to give it a format string.
And then we're going to have a function on it when we get a user's input which is the
string we're going to pass in, we try to get a date out of it.
So say we get down to implementing this function.
At some point in some cases we're going to want to return nil, because, you know, the
user's typed in garbage, it doesn't make sense as a date.
Unfortunately the Swift compiler won't let us do that.
Why won't it let us do that?
It's because the return type here, date, is nonoptional.
Which means it always has to have a value.
It can never be nil.
How do we makes it optional so that it could be nil?
We through a big question mark on the end.
Not inly that big.
That's for effect.
[laughter] And that means the value can be nil.
So this means you have to really reason about the types of your -- the parts of your system
that can be nil and explicitly mark them as being able to be nil.
So things that talk to the network, things that read things from disk, and other bits
you can have this juicy core of nonoptional types which gets rid of this whole class of
bugs.
It's sweet.
So let's start putting some bits together.
They so we're going to put the user's input, they're going to give us a date of birth.
Then we're going to format, we're going to try to get the date from what the user has
given us.
This is the main way we deal with optionals in Swift is called optional binding, if we
do if let, we have a new variable name.
And thin indeed these curly braces here, we definitely have a value for date.
There's other ways of dealing with optional binding, but this is kind of the main one.
So we move on to our final day of week type.
Enum, it's a sum type.
We have one for each day of the week, we can define functions on them.
So we have a switch statement that like if it's Monday through Friday, it's not the weekend,
if it's Saturday or Sunday, it is, and now we can put this in here, so we have our maybe
date.
If we do get a date then we could get our person object, then we can query our person
object, the date of birth, is it the weekend?
So that's the basic run-through of the basic syntax of Swift, there's a lot of more complicated
bits, there's generics, there's more enums, but that's hopefully enough to whet your appetite.
What if you want to learn more?
So Swift was open sourced in 2015, which was quite big
for Apple.
The most interesting part of this is there's this Swift evolution repository.
So every time someone wants to make a change to Swift, they have to write up this detailed
design document saying why they want to make the change, the impact it's going to have
on existing code, because there's a lot of existing code out there, and then the community
will debate it and it will get accepted or rejected, so you can go to this repo and see
this chronological list of all the different proposals that have been made an the ones
that are being made for Swift 4 which will be coming this summer and then another very
exciting thing of Swift being open sourced is it runs on Linux so you can have Swift
on the server.
It's not just for mobile apps.
So there's two big web frameworks, Kitura is made by IBM.
Vapor is another effort.
I know some that are running Swift in production.
Why would you want to do that it's a little bleeding edge.
If you have a bunch of number crunching and a bunch of mobile developers on your team,
you can get them to write a Swift server.
Then you could include it your iOS app, you could include it your server app and there's
a whole bunch of duplication that you've gone away, like magic, so yeah, go out and play
with them, they're really cool.
That is all I have time for.
You can check out Harry's at Harry's.com or you can download our app on the app store.
Thank you.
[applause] >>
MERI: So I owe Neil and apology, because I ask the speakers about a week in advance about
whether there's anything they particularly want me to highlight.
He was like, "The excellence of my mustache," and I forgot to do that in the intro.
So if we could extra round of applause for Neil's mustache.
Thank you.