Swift from a Beginner’s Perspective
With the newly released Swift 5, now is a great time to learn this language.
Do you have an idea for an iPhone app or maybe even a macOS app? Swift 5 is what you want to start learning to get you moving in the right direction. Sure, you can write apps with Objective-C but Swift is becoming more popular and seems to be a much easier language choice overall. Xcode is a free IDE that can get you up and writing your first app within minutes(provided you have macOS).
I’ll be giving you my thoughts on Swift as a beginner starting to learn the language while developing my first iOS app. Many times when you are researching a language you will be receiving opinions from seasoned programmers with a lot of experience in Swift. While this information is invaluable, a lot of that can just go over your head. Whether you have a few languages under your belt or if you are considering Swift as your first language, I believe Swift is amazing.
How can we talk about Swift without first mentioning Xcode? Some people love it, others hate it but after learning and getting used to it’s features over the last few weeks I have to say I thoroughly enjoy it. It’s a beautifully designed IDE with all the features you expect and then some. Managing files and projects with ease, having multiple files open side by side, a great debugger, and even a built-in feature called Playgrounds. In the Playground, you can well, play around. It’s an environment for you to test and experiment with code and see results in real time. This an excellent tool for learning.
Like most languages, you will be able to find the official documentation for the language from the developer. Swift is no exception. Apple has The Swift Programing Language and API Design Guidelines for free. It details everything you need to know about the language and is a great reference for the times you get stuck. New to programming or experienced in other languages alike, these documents will come in handy and are available to you with a touch of a button.
The Elegance of the Language
Swift is absolutely a beautiful language. There are a lot of little things that make this feel very modern. As a beginner with this language, a lot of things instantly stuck out to me. These things might not be specific to the language or overall very important but they have changed the way I think in certain cases. It has also increased my enjoyment of writing in Swift overall.
- Stucts: Structs are one of the named types in Swift that allows you to encapsulate properties(local variables) and methods. If you are familiar with OOP(Object Oriented Programming) they are similar to classes with a few differences. In Swift, structs have no inheritance. This makes structs a little more simple without having to worry about everything you would be inheriting. Also, structs are value types and classes are reference types. When Stucts are passed in an argument, put into an array, or even assigned to another variable it is essentially copied. Structs are everywhere in Swift. Arrays, Ints, Dictionaries, and even Strings are a few examples of Structs. Keeping this in mind can help you to solve some issues you might be experiencing or at the very least change the way you are writing a little.
- Optionals: I have a little bit of a love-hate relationship with optionals at the moment. You will see these everywhere in Swift. Optionals will wrap around a value, for example, an Int, and the data type will be shown as “Int?”. The question mark is truly a great way to show that it’s an optional because you’ll be asking yourself, “Maybe there is an Int in there?”. Which is absolutely correct. An optional is an enumeration with two cases. It is either set or not set. Optional.none is the same as a nil literal and Optional.some(wrapped) means that it is set and there is a value. In most context, you will have to unwrap the optional before you are able to use the value inside.
- Internal and External Parameter Names: This was actually quite strange to me at first but after reading up on it and having some hands-on experience I think it’s quite amazing. Yes, methods have two parameter names. The use of the external parameter named is used when calling the method with the intent on making the method calls read more like English. With well-named variables and methods, it’s so easy at a glance to just look at a line of code and be able to interpret exactly what it does.
- Model View Controller: This is my first experience with the MVC architectural pattern. Basically, it splits your project into three categories modularizing it into specific purposes. I am by no means an expert on this but I believe I have a very basic functional understanding. The model is where all of your business logic resides. The model is created to function independently from the view. The view is where the user experience begins. Everything on the screen that user interacts with or sees is in the view. This is pretty much the UI. Now the controller or the ViewController, as it’s called in Swift. is what goes back and forth between the model and the view to show changes in the model. The controller isn’t typically that many lines of code and if it is maybe you have some things that are more suited to be in the model or a second controller. Typically, the controller is the least reusable part of this architecture since it’s use is fairly specific as opposed to the model which can pretty much run on its own.
I could really keep going about the little things that impress me but I’ll just leave a few more of them here if you are interested in reading up on them on your own.
- Property Observers (didSet/willSet)
- Lazy Variables
- Array Indices
- Countable Ranges
How to Get Started
This is going to be different for everyone depending on learning style preferences.
- You will need to have a computer that runs on macOS. There are certain workarounds such as hackintosh but for the most authentic experience especially when starting out this is your best option.
- Go download Xcode! It is 100% free on the app store. You don’t need to invest anything to get started. The IDE is free. The documentation is free. It’s the professional work environment right at your fingertips. Later on down the line, you will have to pay an annual developer fee to post an app to the store but that’s a problem for future you. If you end up learning Swift and develop an app you have put your blood, sweat, and tears into then investing a little in yourself shouldn’t be too concerning.
- You have Xcode now what? There are many resources out there to get you started. I HIGHLY recommend the free Staford course on YouTube. It’s also available on iTunes. Udemy is another great resources for courses that will take you from start to finish through Swift and the app development cycle. YouTube, Reddit, boot camps, independent websites, there are tons of resources to get you started. Just get started and you will learn a lot on the way.