IOS APp DEVELOPMENT

When You Write Code in Swift, Write Code in Swift

Do not forget convenient methods & constructions Swift provides you with.

Alex Zarr
Geek Culture

--

Swift Logo

Swift has many unique (and not that unique) constructions, methods, approaches that other languages lack. As a result, many people moving to Swift from another language do not leverage its power.

Here is a list of things many people ignore while working on iOS apps.

For case:

In case you have an [Any]array or NSOrderedSet, you don’t need to check the type of each element in if let element = element as? SomeType { inside of a for loop. There is an easier approach:

Don’t forget about enumerated()

Forget about for i in 0..<object.count, you don’t need it in most cases. But if you need to get an index of each element (along with the element itself), use enumerated():

It’s fine to use for i in 0..<object.count when it’s convenient to do so, just keep in mind it’s not the best approach most times.

first(where: ):

filter is one of the most popular methods in Swift. However, from time to time it’s used where it’s not supposed to.

For instance, when you need to get the first element satisfying a condition, you can use first(where: ):

contains(where: ):

The story with this method is almost the same as above:

isEmpty

When you need to check if a collection is empty, don’t check how many elements it contains:

forEach

The forEach method comes in handy often when you want to perform some simple action and don’t need complicated logic with break and/or continue:

Using keyPaths in map, compactMap, filter, etc.

KeyPaths may come in handy in such cases. And don’t forget chaining of methods:

guard

guard is one of the most popular statements but sometimes people forget about it. Remember to use it as it may help you in case you have many potential ifs in your code:

defer

defer is useful too. Everything you put in a defer block will get called after return:

Computed properties vs. methods

Sometimes instead of making a method, you might want to create a computed variable. It depends on the particular context but make sure to choose the correct one. If you need to show explicitly that you’re doing some calculations, use a method, otherwise, consider a variable:

Avoid self when possible

There is no need to contaminate your code with the word self. If it can be omitted, it should be omitted. Use it only when you have to or when it makes your code more understandable.

Don’t rename variables while unwrapping

Use the power of protocol extensions

You can use extensions for default implementations of your protocol’s methods:

Protocols vs. Subclassing

Try to use protocols instead of inheritance. Protocols allow you to make your code more flexible, as you can conform your class more than to just one protocol. Also, structs and enums cannot use subclassing, but they can be conformed to protocols which makes protocols even more helpful.

Structs vs. Classes

Try to use structs over classes when possible. They are safer in a multi-thread environment, faster, they have a default init method and many other benefits. But understand the difference and keep in mind that structs are a value type while classes are a reference type, meaning that each instance of a struct has its own unique copy of it, while each instance of a class has a reference to one single copy of the data.

Link to the official documentation explaining how to choose between them is here: https://developer.apple.com/documentation/swift/choosing_between_structures_and_classes.

Use the above-mentioned suggestions, especially if you’re new to Swift. And feel free to share your methods, approaches, ideas that help you truly use Swift while writing your code in Swift, and I’ll include them in the next part.

--

--