Sitemap
Martinsoft

Hello! Welcome to the Martinsoft blog! We share tech news, mobile app development stories, and help to make tech less daunting.

Learn a Language: Swift

13 min readSep 12, 2018

--

Learning to code requires a HUGE investment of time and energy. For newcomers it’s also difficult to know where to start because there are so many languages to choose from. Through my Instagram account I’m often asked:

“Which programming language should I learn?”

I wanted to be able to give a better answer than “it depends”, so with this Learn a Language series, I hope to build a resource that’ll help people decide for themselves which language they should invest their time in learning!

With help from other developers across the tech community, each article will give an overview of a popular programming language, from a developer experienced in that language. We’ll look at what each language is good at, what it’s not so good at, and the tools/resources that can help you get started.

In this article we will be looking at Swift!

However! We’re doing things slightly differently this time! 😃 Although I work with Swift all the time, I didn’t want the article to be only about my experiences. So, I have teamed up with the amazing Carmen Jimenez aka @carmencodes_ who has recently started learning Swift! Together we’ll both be sharing our experiences and resources, to give two different perspectives on Swift!

Carmen aka @carmencodes

How long have you been working with Swift?

[Carmen]

I’ve been working with Swift for about two months.

[John]

I first started looking at Swift shortly after it was announced in 2014, but it was probably about a year or so later that my clients were seriously starting to look at it, so it’s been about 3 years. Before that, though, I’d been an iOS developer using Objective C since about 2010.

What made you choose to work with Swift?

[Carmen]

I didn’t choose Swift, Swift chose me! Jokes aside, when the idea of building my first app came to mind without a doubt I decided to start learning iOS development. I think it’s just because of pure passion since I love Apple.

[John]

Similar thing! I’ve always been an Apple fan and was instantly drawn to the idea of building mobile apps when the first iOS SDK came out, but it was a couple of years later that I really got into it. After many years of working in Objective C, Swift came along and made iOS/macOS development much easier to understand. It uses more modern programming paradigms, and often allows you to expressive ideas in a more concise, type-safe manner, so it was a really nice thing to move into.

What sort of things is Swift well-suited for? When should someone consider using Swift for their project?

[Carmen]

If someone is looking to build software for the Apple ecosystem, Swift is the de facto language of choice for starting new projects in macOS, iOS, watchOS and tvOS. The goal of Swift is to create the best available language for uses ranging from systems programming, to mobile and desktop apps, scaling up to cloud services. Most importantly, Swift is designed to make writing and maintaining correct programs easier for the developer.

[John]

As mentioned, Swift was introduced as an alternative to Objective C, which for many years had been Apple’s programming language of choice for building apps for the macOS and iOS platforms. It makes development much easier because the syntax and style of programming is more similar to other modern languages such as Java / JavaScript.

If you are interested in building native apps for the Apple platforms — iOS, macOS, tvOS, watchOS — Swift should be the language for you.

If you also know other languages, how does Swift compare to these?

[Carmen]

  • Javascript: There are numerous similarities between both JavaScript and Swift. There are some minor differences but their essence is the same. As compared to JavaScript, Swift provides a lot of advancements in syntax. Of course (arguably) it can be said that the more features a language provides, the more trouble it can create. Since then you need to know most of the syntax in order to program effectively. In contrast, JavaScript is relatively simple with a very limited syntax, but it has its shortcoming as well.

However, given the similarities in syntax between both these languages, if you are a JavaScript programmer, you can learn Swift really fast and pretty easily. Bottom line — if you know JavaScript, you can learn Swift swiftly.

  • C#: These languages have different primary uses. Swift is used primarily to develop apps for Apple devices (Macs, iPads, etc). And while C# may be used for client-side development, it is also extensively used for developing server-side applications. C# has been around longer, but it has continued to evolve and is certainly not approaching obsolescence. To be clear, C# is used for way more than server code. It’s Microsoft’s language (in much of the same way that Swift is Apple’s), and it’s used greatly to develop Windows and Windows apps. It’s also used in a lot of game engines, such as Unity. Web servers are one of the things it can do, not what it’s made for.

[John]

I have worked in many different languages over my career, but Swift keeps a lot of the nice elements that I’ve seen in other languages:

  • It’s a strongly typed language, similar to Java, meaning that it tries to avoid runtime errors by performing more type-checking at compile-time. This means, for example, that the language will prevent you from passing an Integer to a function that expects a String.
  • Swift also has extensive handling for ‘optionality’ — which is another attempt to avoid runtime crashes caused by null-pointer exceptions (where a variable unexpectedly ends up having a null value). Swift code requires you to explicitly state where a value might be null (a so-called “optional”), and forces you to handle this whenever using that value in the code. Traditional languages such as C/C++ and earlier versions of Java required you to remember to check if a value might be null. Swift has built-in mechanisms to ensure that these checks are handled at compile time.
  • In terms of the syntax, Swift is very similar to other C-derived languages — C, C#, Java, JavaScript, but extends on these particularly in terms of the additional types that it supports, and the elegant ways that it implements enums (which are very powerful), structs (for immutability) and protocols (similar to interfaces in Java).

Are there any types of project / task that Swift would not be a good choice for?

[Carmen]

Swift is a powerful and intuitive programming language developed by Apple, and initially aimed at development for Apple’s platforms, therefore you can not easily develop an Android app with the help of Swift. Although work in progress, there has been open-source initiative to get Swift code to work on Android.

[John]

As a language, Swift was initially released by Apple but then subsequently made open source. This is important because it means that anyone can contribute to the language, and the language is open to being ported to other platforms.

That said, Swift in its current stage is still primarily a language for the Apple platforms. There are projects working to bring Swift to Linux — which is something I’m really interested in — which could eventually mean a full-stack mobile app could be written end-to-end in Swift, but this is still fairly uncommon and it is still much more common to see Java / Ruby / Javascript on the backend while the iOS app would be written in Swift.

If you are looking to build web apps, Windows apps, or Android apps, Swift is probably not going to be your first choice of language!

What do you particularly like about coding in Swift?

[Carmen]

What I like about Swift is that it is easier to read and write. It is very concise, which means less code is required to perform the same task, as compared to Objective-C.

Another thing I like about Swift is its safety. Its strong typing system and error handling prevents code crashes and errors in production. Thus, Swift has a shorter feedback loop, allowing you to see the errors in the code instantly and fix them on the fly, greatly reducing the time and effort needed for bug fixing and eliminating the risks of deploying low quality code.

I like the built-in collections, particularly the Set type. Normally, I’m stuck with array and dictionary types, which are fine. However, sometimes I need to do set operations, which on arrays are difficult. It’s good having the option to have an unordered list with set operators.

[John]

I’ve already touched on some of the language features that are particularly nice with Swift:

  • Clearer syntax and strong type checking at compile time.
  • Optionality checking and (if implemented properly!) fewer crashes at runtime due to null pointer exceptions.
  • The support for Playgrounds — which is a dynamic way to run Swift code without having to set up an entire project. A playground executes your code after every change, and shows you the results inline. This is great for quickly testing an idea or implementing some UI control, without having to code it within a large project (which would take longer to build / test etc). Playgrounds are even available for iOS on the iPad — a great way to start learning Swift!

What are the things that frustrate you when working with Swift? What possible sources of pain or frustration (if any!) should people be aware of before they start learning Swift?

[Carmen]

I like Swift a lot and there aren’t many things that frustrate me when working with it but I must say it is often hard to find the right tools to help with the certain tasks. Moreover, the official Apple IDE, Xcode, lags in terms of tooling and support for Swift. I’ve seen a lot of reports from developers with issues in regards to syntax highlighting, autocomplete, refactoring tools, and compilers.

[John]

Get John Martin’s stories in your inbox

Join Medium for free to get updates from this writer.

Most of my frustrations lie with Xcode which, although is improving, still has a number of issues (some of which Carmen touched on above).

  • Code completion and syntax highlighting will often stop working, making it very difficult to work with. (Xcode uses a background service called SourceKit which seems to be notoriously unreliable).
  • Xcode’s refactoring tools are very primitive compared to other IDEs and again pretty unreliable.
  • Playgrounds are also pretty unstable, particularly if you are running an iOS playground. Running the playground in macOS rather than iOS mode seems to work much better if you are only using core swift and none of the frameworks used by iOS.
  • As a beginner, it was initially quite difficult to get my head around Optionals and how to elegantly work with them. Once you learn how to use them properly though, things become much easier.
  • The syntax for closures (blocks of code (like a mini function) that can be passed around and treated like variables) is often a bit confusing but has got a bit easier with each version of swift. If you get stuck I highly recommend http://fuckingclosuresyntax.com — and if you want to laugh compare this with the Objective C equivalent: http://fuckingblocksyntax.com to see how far we’ve come in the last few years!

What dev tools do you use when working with Swift? What other tools are also popular?

[Carmen]

These are some of the tools I use when working with Swift:

  • XCode — this is the original IDE made by Apple, but there are alternatives. I recommend you try Xcode out first before deciding to move: there are enough plugins that you can probably customise your experience to be exactly as you want it. Some of the other IDEs are Code Runner and Swifty.
  • Playgrounds: Swift Playgrounds is an iPad app that helps you learn and explore coding in Swift. Engaging lessons and challenges help students learn the core concepts of coding by writing real Swift and iOS code in an interactive environment designed for touch.

[John]

  • Thankfully Apple package up most of the dev tools you need into Xcode — their IDE for Apple developers. This includes support for building apps for all the Apple platforms, and includes the Playground editor for interactive coding.
  • There is an alternative IDE called App Code which offers full support for Objective C and Swift code editing, but I’ve not yet had a chance to look at it.
  • Other than Xcode and the terminal (I use iTerm2), there aren’t many other tools I need to work with Swift! The other tools I usually have open are visual tools such as Invision, Zeplin or Sketch, holding the designs for the app I’m working on.
Follow @carmencodes_ on Instagram where she shares her journey as a developer learning Swift!

Which resources would you recommend for people looking to learn Swift?

[Carmen]

  • Your first point of visit should be the official iOS Dev Center — Apple Developer
  • Then Apple’s own The Swift Programming Language eBook.
  • Treehouse: Treehouse provides one of the best iOS and OS X courses in the industry. They teach the basics, building a well laid foundation before moving on to more topics before helping you built an iOS app.
  • Udemy: iOS 11 & Swift 4 — The Complete iOS App Development Bootcamp by Angela Yu. Angela is a very good instructor. She explains Swift core concepts, Xcode and tools quite clearly and provides relevant examples, in a humorous way most of the time.. and that is great because as a learner you understand and grasp the subject quite easily in my opinion.
  • Ray Wenderlich — A Swift Quick Start Guide: His resources provide incredible amount of useful resources for learning Swift. This is a good place to kickstart your Swift learning.
  • iOS Dev Weekly — Weekly newsletter (probably the most popular one) about iOS development — hot news, dev tutorials, tools, design and marketing articles — everything that every iOS programmer needs.

[John]

Carmen has given some great suggestions above. I definitely recommend starting with Apple’s own eBook The Swift Programming Language — it’s the only real reference you need to the language itself. For general learning of Swift and iOS I would add to this:

  • Apple’s Swift Playgrounds app for iOS. If you have an iPad, you can start playing with Swift straight away. They have great introductions and templates that you can work through.
  • I highly recommend the Stanford University course CS193P: Developing iOS 11 Apps with Swift. This course is amazing and is updated every year with the latest best practices and APIs. This was the one course that really helped me transition to iOS from Java and I am still amazed that this course is available for FREE! You can watch on iOS via the iTunes U app or through the podcasts section of iTunes on the desktop.
  • I’ve also heard great things about DevSlopes although I’ve never personally used their courses.

What are the popular libraries or frameworks that people should also consider learning with Swift? What do these libraries / frameworks do?

[Carmen]

  1. Alamofire: When you want to abstract away and simplify networking in your app, Alamofire is the way to go. Alamofire is a HTTP networking library, built on top of NSURLSession and the Foundation URL Loading System. It nicely wraps networking mechanisms in an elegant and simple Swift interface.
  2. SwiftyJSON: Asuper-simplified JSON parsing library that gives you clearer syntax than the built-in iOS libraries.
  3. SnapKit: An Auto Layout library that simplifies writing auto layout in code with a minimal amount of code needed without losing readability.
  4. Eureka : Eureka is a powerful library that allows developers to rapidly create form interfaces for user input. It consists of rows, sections and forms. If your app contains a lot of forms, Eureka proves to be a real time-saver.
  5. ObjectMapper: If you’ve ever written an app which downloads information via an API, you’ve probably spent a lot of time writing a code to map a response to your objects. ObjectMapper helps you convert a JSON response into your model object, and vice versa. In other words, it helps you map JSON to objects, and objects back to JSON. Nested objects are supported as well.

[John]

Again some great suggestions there from Carmen! I usually try to avoid adding too many external libraries to a project as there is an overhead in managing these dependencies. Instead I highly recommend any iOS developers learn some of the key Apple technologies and frameworks that come with iOS:

  • AutoLayout — not so much a library as a set of technologies that allow you to design layouts that will adjust and adapt to different screen sizes and orientations. Understanding AutoLayout can be a challenge as there is a lot it can do, but once you understand it you can save a lot of time building UIs that work across all device types with minimal coding.
  • Core Graphics — used for drawing custom graphics to the screen. If you are implementing custom controls for your app, you will almost certainly need to use Core Graphics. And if you are looking to add flair to your app then understanding the basics of Core Animation can also make a huge difference to your app!
  • Core Data — a persistence framework for storing objects in a database. There are many alternatives to Core Data but understanding this framework will certainly help if you are working with other people’s code.

Some of the common external libraries I’ve seen on client projects include:

  • MBProgressHUD — offers a quick way to show an activity indicator with/without a message beneath it.
  • SDWebImage — extensions to UIImage/UIImageView to support asynchronously downloading and caching images.
  • Realm — a persistence framework for storing/querying/retrieving objects. Apple provides a framework called Core Data for this, but Realm’s implementation is often easier to understand, and they also provide synchronisation services to sync changes between devices.

Anything else you want to say about Swift?

[Carmen]

The future of Swift mostly depends on the future of Apple, which so far seems very bright. Since Apple will be focused on improving Swift in the years to come, you can expect it to stay relevant for a good while.

[John]

Swift is a really nice language to work with, and if you have any interest in developing for Apple platforms then it is definitely the time to take a look!

Thank you so much Carmen! Where can people find you online?

[Carmen] I’m on:

  • Twitter @carmencodes_
  • Instagram @carmencodes_

Thank you so much for reading! I hope you found this article useful!

If you know Swift and have suggestions for other resources that beginners should know about, feel free to leave them in the comments!

Other languages will be coming soon!

You can find me:

Instagram: @martinsoft

Online: https://martinsoft.net

I also co-host @meetupandcode, a regular meet-up in London for devs with any level of experience. Join us!

--

--

Written by John Martin

Full-stack developer with a focus on iOS. Founder and freelance dev @martinsoft. Passionate about all things Apple, and using tech to make life better.

Published in Martinsoft

Hello! Welcome to the Martinsoft blog! We share tech news, mobile app development stories, and help to make tech less daunting.