Join Lee Brimelow for an in-depth discussion in this video Introducing the Swift programming language, part of iOS 8 SDK New Features.
- So without the question, the biggest new feature in Xcode 6 is the introduction of a brand new programming language, known as Swift. Which we're going to go over right now. Now first, before we start, some important points about Swift. First, using it is entirely optional. Apple is not forcing you to now write your applications in Swift. You can still you Objective-C to write both iOS and Mac applications. Now you can combine both Swift and Objective-C in the same project. So say you have an Objective-C based iOS project, you can write new classes using Swift and include them in your project.
But it definitely doesn't look like Objective-C. So in Swift, we don't have all the square brackets that we're used to when doing iOS development. So when we're creating properties in Swift, we can either create a variable or a constant. So here is a variable declaration. I'm using the var keyword, creating a variable called str, and setting it equal to a string value. And obviously, since this is a variable, I can change this at any time. To create a constant, we use the let keyword.
So we say, if we want to create this as a constant, let str equal "This a constant." And of course we can't change this value any more after this point. Type inference is one of the biggest and most powerful features in Swift. So when I declare a variable, you can see here I'm saying var name is equal to a string value. I didn't have to tell the compiler what type this variable is. Because the compiler is able to look at the value that I assigned to that variable, and it's smart enough to figure out, "Hey, I get it.
"That's a string." Now there are cases where you either just might what to, or have to, provide a type value. And in that case, in Swift we do that by using a colon after the variable name with the type of that variable. Now parentheses and semicolons, you've probably noticed from what you've seen already, we don't see semicolons. Well, they are optional in Swift. And also in some cases, parentheses. So here you can see a for loop, where I'm iterating over an array. Typically, we would need to put in parentheses around item in myArray.
Well, in Swift that is optional. Object initialization. So here is an example of creating an array constant. So I say let arr equal to. And here's how we instantiate a new object in Swift. We simply give the class name, and then open the parentheses. And then use one of the constructor overload. Where as in Objective-C, we would do NSArray alloc initWithObjects. So this new syntax for initializing objects is a lot more compact.
Function declaration. So here is an example of a function called sayHello. And it takes a single argument, which is a string. So we create a function using the func keyword, then we give the name of the function, and then in parentheses, any arguments that we're going to send, with types. And then to denote the return value of the function, we use the dash greater than symbol, to kind of create an arrow saying that this function is going to return a string. Tuples are a very powerful feature in Swift, because they allow you to return multiple values from a function.
So here is the function signature for essentially a min max function, which takes an array of integers, and then it returns the smallest integer in that array and the largest. And you can see with tuples, we can define multiple values that we can return from a function. Class definitions. So when we're defining classes in Swift, this is what it looks like. So we have our imports at the top, we use the class keyword, then give a name for our class, colon. And then if we're subclassing a certain class, like here we are, UIView, we put that.
And then if we're implementing any protocols, we put a comma and then the protocols that we're implementing in this class. Now one of the biggest things to take away from this is there are no header files. Where in Objective-C, we would have a separate dot H and dot M files, in a class definition, there's only a single file, a dot Swift. So, we no longer have to use header files when we're using Swift code. Optionals are one of the most important concepts in Swift.
So here, the concept of an optional is, let's say I'm declaring a variable as a property in my class. Called firstname of type String. Well, maybe I don't know the value of firstname as I'm writing my code. It needs to be perhaps entered by a user. So I define it as being an optional using the question mark after the type. Now what this means, this variable can have one of two values. It can either have a real string value or it can be nil. So again, this idea of creating an optional is saying, "This could have a value or it might actually be nil." So now how do we use optionals inside of our projects? Well, taking that firstname variable again, let's say I wanted to use that somewhere.
We can do something called forced unwrapping by putting the exclamation point after the variable name. And that's basically saying, "Look, just give me "what's assigned to firstname. "If it's a real value, fine. "If it's nil, fine. "And you know what, I'll do the work of "checking to see if it's nil, and I will use this safely." There's a better approach, called optional binding, and here is what I'm essentially doing, is saying if let fname, I'm creating an constant, is equal to firstname.
And what this is saying is if firstname actually has a real value and it's not nil, then execute this block of code and then I'm able to use that fname constant to reference the firstname variable. And this is a safe way of checking and using your optional variables. Now I want to point out here that Apple has released two free iBooks that are excellent to help you to get started with the Swift programming language. So if you go to the iBook store, and just search for Swift, on the left we have "The Swift Programming Language" which is a really in depth programming manual, and then on the right we have "Using Swift with Cocoa and Objective-C" which talks about specifically how to combine the two languages together in a project.
Now of course this was just a really quick overview of Swift. If you want more in depth training, definitely look at some of the other Swift courses here on Lynda.com.
- Getting started with Swift
- Using Swift playgrounds
- Exploring unified storyboards
- Understanding how extensions work
- Creating a Today widget
- Using the Photos framework
- Controlling graphics with the Metal framework
- Overview of HealthKit and HomeKit
- Combining Swift and Objective-C