10 iOS Development Tips for Swift and Xcode

Use these productivity tips to become a more efficient iOS developer.
headshot of author Artturi Jalli
Artturi Jalli
Expert Columnist
June 14, 2022
Updated: June 15, 2022
headshot of author Artturi Jalli
Artturi Jalli
Expert Columnist
June 14, 2022
Updated: June 15, 2022

Do you want to become the next top-notch iOS developer? These tips will help you save time and resources so you can spend more time focusing on the parts of code that really matter. This list is in no particular order.

10 Ways to Become a Better iOS Developer

  1. Check if all items meet a criterion.
  2. Use the where keyword.
  3. Swap two variables without a third with tuple destructuring.
  4. Remove the init() method from a struct.
  5. Rename properties .
  6. Use multi-cursor edit.
  7. Extract code to a method.
  8. Extract to variable.
  9. Add missing switch cases.
  10. Use automatic code styling with SwiftLint

 

1. Check If All Items Meet a Criterion

When you want to check if all elements satisfy a condition in a collection, you would probably loop through the list and accumulate the result by checking each element separately.

For example:

let ages = [20, 28, 30, 45]
var allAdults = true
for age in ages {
    if age <= 18 {
        allAdults = false
        break
    }
}
print(allAdults)

Output:

true

However, this is such a common task that Swift developed a native function for checking if all the elements satisfy a condition: the allSatisfy() function.

The allSatisfy() function takes a closure as an argument that applies a condition check for each element in the collection.

For example, let’s repeat the above program that checks if all ages are 18 and above.

let ages = [20, 28, 30, 45]
let allAdults = ages.allSatisfy { $0 >= 18 }
print(allAdults)

Output:

true

To clarify the code, the allSatisfy() function iterates through the collection and stores each element to $0 one by one to perform the check.

More From ArtturiWhat Is the @ Symbol in Python and How Do I Use It?

 

2. Use the Where Keyword

In Swift, you can make your code more readable by using the where keyword to check a condition.

Normally, you would probably use an if statement to check if a condition holds. For example, let’s loop through an array of numbers and print only the odd ones.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers {
    if number % 2 == 0 {
        print(number)
    }
}

Instead, you can use the where keyword to chain the if-check into the for loop declaration:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers where number % 2 == 0{
    print(number)
}

 

3. Swap Two Variables Without a Third With Tuple Destructuring

In Swift, you can use tuple destructuring to pull apart values from a tuple into separate variables.

let (a, b, c) = values

Let’s see how this helps.

Normally, you can refer to the elements of a tuple using the dot notation by specifying the name of the element.

For example, given a tuple that has x, y, and z coordinates, you can extract those coordinate values to separate variables by:

let coords = (x: 1, y: 3, z: 2)
let x = coords.x
let y = coords.y
let z = coords.z

You can use the tuple destructuring to do the exact same thing with less effort.

let coords = (x: 1, y: 3, z: 2)
let (x, y, z) = coords

An interesting application of this feature is using tuple destructuring to swap two variables without a third one.

For example:

var a = 1
var b = 2
(a, b) = (b, a)
print(a, b)

Output:

2 1

This is a classic interview and exam question. Make sure you nail it!

More Tips for Software Developers10 Myths About Programming and Software Development

 

4. Remove the init() Method From a Struct

If you have dealt with classes in Swift, you know you need to implement the init() method to initialize the class instances.

This is also possible with structs. However, it is not necessary if you only want to assign initial values to the properties of the struct.

Let’s create a fruit structure with an init() method.

struct Fruit {
    let name: String
    let color: String
    init(name: String, color: String) {
        self.name = name
        self.color = color
    }
}
var banana = Fruit(name: "Banana", color: "Yellow")
print(banana.name, banana.color)

This works because structures automatically create a memberwise initializer method to the structure.

Remember that this does not apply to classes! You always need to implement the init() method into a class in Swift.

8 Swift Tips to Level Up Your Swift Programming Fast!

 

5. Rename Properties — Xcode Refactoring Tool

In Swift, there is an amazing built-in refactoring tool. You can use it to avoid some repetitive manual work.

The next several tips are related to using the Xcode refactoring tool.
 

Rename a Property Using the Rename Feature

The rename feature finds every place where the highlighted text occurs and renames it how you want.

Edit all references of the number variable everywhere you use the rename feature.
 

Edit All in Scope

You can also rename a property in Swift in a specific file by command-clicking and renaming the property like this:

This is different from the rename function in that it only changes the property name in the current file, not other places in the codebase.

Looking for More Tutorials? We Got You.Create React App and TypeScript — A Quick How-To

 

6. Use Multi-Cursor Edit — Xcode Refactoring Tool

Did you know you can add multiple cursors into your Swift code using Xcode’s multi-cursor feature?

To activate the multi-cursor feature, press shift + control + left mouse click.

You can easily edit code with the multi-cursor in these four ways:

  • shift + control + left mouse click creates a new cursor for each click.
  • shift + control + arrow up creates a new cursor to one line above.
  • shift + control + arrow down creates a new cursor to one line below.
  • option + mouse drag creates new cursors on new lines wherever you drag the cursor.

 

7. Extract Code to a Method — Xcode Refactoring Tool

To improve code quality, you should extract longer pieces of code into separate methods.

To do this, you can copy-paste a piece of code to a function manually.

Sine this is such a common practice, there is now a built-in refactoring feature called extract to method in Xcode that does the job.

For example:

 

8. Extract to Variable — Xcode Refactoring Tool

Similar to the previous example wherein you extracted a piece of code into a separate method, you can extract an expression into a variable.

To do this, use the built-in extract to variable feature in Xcode.

For example:

You can also extract all identical expressions to a separate variable using the extract all occurrences feature.

Find occurrences of UserDefaults.standard.array(forKey: “Names”)! and create a variable for them.

 

9. Add Missing Switch Cases — Xcode Refactoring Tool

If you use the default case in a switch statement the Swift compiler does not show errors if a case isn’t covered. This means there can be a case you should check but you forgot to do so.

This is where the Xcode built-in refactoring tool helps. You can use it to expand the missing cases.

For example:

 

10. Use Automatic Code Styling With SwiftLint

Understanding code can be difficult. To make the process as smooth as possible, the code has to be of high quality. One way to enforce code quality is by following some common code styling guidelines.

But who is there to make sure your code follows those best practices? And what even are those best practices?

This is where a linter helps you. You can use a linter to enforce a common coding style and best practices. You can also specify team-specific best practices for your project.

Linters can:

  • Show warnings
  • Fix the code automatically to silence the warnings

Here is an example of a linter showing warnings in code:

iOS-development-tips-swift-xcode

In Swift, the most common linter is called SwiftLint. It’s easy to set up and you can learn to do it in five minutes. A linter is something you are 100 percent likely to use if you develop software in a team.

I hope you found something new and interesting to your development routine.

Happy coding!

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us