Code Challenge: Learning Swift
SendGrid TeamDay 1 – Learning Swift
As part of the 15-Day Code Challenge that I’ve come up with, I’m learning about 15 new technologies in 15 days. The first is Swift, Apple’s new coding language.
I’ve always liked developing for mobile, but I haven’t had the time to explore and play around with Swift until today. In this post I’m going to go over my experience learning Swift and building my first app.
For this blog post, I have been primarily reading the ebook that Apple released. It’s a great resource and I highly recommend it for learning Swift.
Step 1: Getting Set Up
The first thing I found was that you need to have a Mac. This doesn’t have to be an expensive, shiny, new system, but still, a Mac is required.
There are other third-party applications that will let you develop IOS apps on other systems such as PhoneGap, Appcelerator, and Xamarin, but for this post I’ll be focusing on the native Apple developer environment.
If you want to follow along with what I’m doing, your system will need the following:
- OSX 10.9.4 or greater and you need to have 2.46GB of free space to install the latest version of Xcode, according to Apple.
- Xcode from the App Store app on your Mac. It’s free.
What is Xcode?
I’m familiar with Xcode, but just in case anybody reading this isn’t, its Apple’s development environment. In Apple’s words Xcode is, “…the center of the Apple development experience.”(https://developer.apple.com/xcode/ide/) It’s an integrated development environment that lets you build the visual interface of your apps and all the corresponding logic.
Step 2: Learning Swift
I’ll dive further into Xcode in a bit, but before we do, lets play around with the Swift programming language first.
There are two ways you can do this. First, you can open up your terminal window and type in swift at the command prompt:
The other way is what I prefer, and that’s using the Xcode Playground:
You can get to the screen above by choosing a new playground on the dialogue window that displays once you load Xcode. This is where we can go over the basics of the Swift programming language.
I highly recommend you use the playground screen and start playing around with the Swift language. I previously coded in Objective-C and have found Swift to be much easier and intuitive and more of a common C syntax.
The basic operators are the same as C. One interesting difference is the “=” assignment operator doesn’t return any value. So this prevents the common forgetting the second equal “==” when comparing values in an if statement.
Another interesting fact is you can use emoji for variables.
In the example above you can see I used two different emojis to represent “Face” and “book” and then I printed the concatenated version.
Control Flows
If statements and for loops are much more intuitive in Swift and pretty much what I expect from any language. Just a note, parentheses aren’t required when you give a condition statement.
Here is an example of the for-in statement in the book:
for index in 1…5 {
println(“\(index) times 5 is \(index * 5)”)
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25
If you want to get a better grasp of Swift, visit: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
Step 3: Creating my app!
Now that I got caught up with how the language works, I’m ready to build my app! I’ve created a video below creating a first Hello World application:
Be on the lookout for my next post on Monday, November 24th to see what I’m tackling next! You can also read more about this challenge and get a list of completed technologies in my intro post, 15 Days of Code Challenge.