www.toptal.com

The Mistakes Most Swift Developers Don't Know They're Making | Toptal®

2/19/2013Updated 5/14/2025

Excerpt

Coming from an Objective-C background, in the beginning, I felt like Swift was holding me back. Swift was not allowing me to make progress because of its strongly typed nature, which used to be infuriating at times. Unlike Objective-C, Swift enforces many requirements at the compile time. Things that are relaxed in Objective-C, such as the … In the beginning, you might want to force unwrap everything, but that will eventually lead to crashes. As you get acquainted with the language, you start to love how you hardly have runtime errors since many mistakes are caught at compile time. Most Swift programmers have significant previous experience with Objective-C, which, among other things, might lead them to write Swift code using the same practices they are familiar with in other languages. And that can cause some bad mistakes. In this article, we outline the most common mistakes that Swift developers make and ways to avoid them. … Second is forcing unwrap using the `!` operator, or using an implicitly unwrapped optional type (e.g. `String!`). If the optional is `nil`, forcing an unwrap will cause a runtime error and terminate the application. Further, attempting to access the value of an implicitly unwrapped optional will cause the same. … ## 3. Using `self` Everywhere Unlike in Objective-C, with Swift, we are not required to use `self` to access a class’ or struct’s properties inside a method. We are only required to do so in a closure because it needs to capture `self`. Using `self` where it’s not required is not exactly a mistake, it works just fine, and there will be no errors and no warnings. However, why write more code than you have to? Also, it is important to keep your code consistent. ## 4. Not Knowing the Type of Your Types Swift uses *value types* and *reference types*. Moreover, instances of a value type exhibit a slightly different behavior of instances of reference types. Not knowing what category each of your instances fit in will cause false expectations on the behavior of the code. … When you get used to Swift and go back to Objective-C, you will notice the difference. You’ll miss nice features Swift offers and will have to write tedious code in Objective-C to achieve the same effect. Other times, you’ll face runtime errors that Swift would have caught during compilation. It is a great upgrade for Apple programmers, and there’s still a lot more to come as the language matures.

Source URL

https://www.toptal.com/swift/top-swift-development-mistakes

Related Pain Points