Fri, Feb 26, 2016
Recently, after doing web development for years, I decided to move away from that world and dive into something new (for me), iOS development. In this blog post I will try to give a very short summary of what resources I used to get me started and why they were useful, as well as talk about some interesting features Swift has to offer.
Step one was learning the language. I had never worked with Objective-C or Swift before, so I needed to learn at least one of these. As the industry seems to be moving towards the latter, I decided that the first thing I should do was learn the Swift programming language.
While Apple's "The Swift Programming Language" is a must read for everyone, it is a bit on the long side. Let's face it, everybody wants to be able to do some coding right away, thus, I read Ray Wenderlich's "Swift 2 Tutorial: A Quick Start". Although far from thorough, it's a short guide to introduce you to the language and give you the very basics. While it will not make you an expert, it will teach you enough to start coding your first project.
It is also a good idea when learning a new language to read a style guide, as to give you an idea of things that can often be considered a good idea (or not for that matter). Github's "Swift Style Guide" is a good place to start. It's short, clear and following it will help your code look a lot cleaner. Swiftlint is also a good tool to use to lint your code and make it more readable to others and also integrates nicely into XCode.
As for iOS itself, Apple's "Start Developing iOS Apps" is an excellent guide to get you started with iOS development. While far form deep, it covers a lot of the basic concepts that are needed. I highly recommend it, as it gets you coding right away but also provides good and concise explanations without getting tedious. As a bonus, it also familiarizes you with XCode and Swift.
Although not updated for the latest iOS version, Stanford has an amazing course here. It is very thorough and goes through most topics you will need to eventually go over to build your dream app.
Although few, these links are enough to keep anyone busy for a while. The Internet is filled with tutorials and articles about any topic imaginable, but what kind of person would I be if I just sent you to Google? The awesome-swift and awesome-ios lists aggregate a ton of useful resources for a ton of different topics. If you're stuck or want to know more about a topic in particular I suggest you take a look at them to find help.
While not a feature of the language per-se, Playgrounds are an extremely useful tool when developing. From Apple's Developer Library: A playground is an interactive Swift coding environment that evaluates each statement and displays results as updates are made, without the need to create a project.
A playground is basically a REPL that allows you to try Swift code on the fly. This is similar to what interpreted languages such as Python offer, but that is usually missing when working with compiled languages such as Swift. Playgrounds are extremely useful for prototyping code, as well as trying out snippets of code written by others. Not only that, it also includes some functionality that is missing from most REPLs such as multi line editing. It is also not just a text-only tool, it can also draw graphics, so it can even be used to prototype UI components and animations as it supports UIKit.
August 2018: Please note that this post was written for an older version of Swift. Changes in the code might be necessary to adapt it to the latest versions and best practices.
If you work with statically typed languages, you are probably used to specifying a type for each new variable you declare. This is (except for very specific cases) not needed in Swift, as the type of the variables will be inferred for you, this means that instead of doing something like:
you can simply omit the type and write:
While in this case it is not particularly advantageous, type inference really shines when dealing with longer types like collections with parametrized types or functions as return types.
Enums in Swift are slightly different from your average programming language.
From the documentation: Enumerations in Swift are first-class types in their own right. They adopt many features traditionally supported only by classes, such as computed properties to provide additional information about the enumeration’s current value, and instance methods to provide functionality related to the values the enumeration represents. Enumerations can also define initializers to provide an initial member value; can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.
The documentation is pretty clear, and it's obvious that enums are very powerful in Swift. Something that isn't mentioned though is associated values. Basically Swift allows associating values to each enum case.
The Swift documentation provides the following example which showcases this feature:
This means that each UPCA code has four integer values associated to it, and each QRCode a String. In a switch statement, these values can be then "unpacked". The documentation also shows the following example:
With property observers it is possible to take actions before (willSet), or after (didSet) the value of a property is set.
Take this an example, the when the
progress property of a progress bar is updated it is redrawn,
Getting into iOS development isn’t hard as there are a ton of really good resources to learn from. On top of that, Swift is a great language to work with, so if you were on the fence about whether to learn iOS development or not, please do 😁!