Some Tips to Improve Swift Code (Part 2)

Tram Ho

Part 1 alone has introduced some tips to help improve your code lines, make them easier to read and safer.

In the previous section, I have listed some Tips, today I will review the remaining Tips.

8. Loops

It’s been explained pretty well, isn’t it, the looping syntax and the results are listed next to it.

9. Use Enumerations

Using an undefined string will confuse us with coding, as well as mainternance. With option 2, the coding will become easier and more self-explanatory right!

10. Use Callback to examine a Completion Handler

As we can easily see, both of the above examples can interact directly with the returned data.

11. Provide a default value for the variable

In this example, we see both options for default values ​​when the textInput value is nil, but obviously method 2 makes the code lighter and more accessible.

12. Store common constants in a file for easy access

Storing static constants value at one point makes it easier to manage.

To get value:

13. Automatic Reference Counting

I think every developer should read through the ARC (Automatic Reference Counting) documentation .

ARC is used by Swift to monitor and manage memory. Because in some cases the reference is still kept.

Consider the following example for clarity. For example we have 1 class Person:

We create 3 variables. Each variable is an optional, and the initialization value is nil:

Next, we assign ref1 as a Person:

Next, we assign ref2 and ref3 by ref1 => ref2 and ref3 with ref1 pointing to the same reference:

Now, we can see that the 3 variables ref1, ref2 and ref3 all point to 1 Person object, We can consider the first 2 variables nil, because ref3 still keeps the reference to Person in memory:

Finally, to de-initialize the Persion object, we consider ref3 to nil and the final reference is destroyed:

14. Provide a default value for function parameters.

It’s pretty obvious isn’t that, we just need to provide a default value for input parameters.

15. Encode / decode Struct from memory through UserDefaults.

The file above demonstrates the usefulness of this approach.

First, we need to create a Struct TaskItem that inherits Codable, which allows us to encode / decode through a Serialized Format (eg JSON format).

Further, you can see that, inside the retrieveData () function, we are using guard statements and an if let statement to check if UserDefaults contains an existing array of TaskItems.

If no such array exists, we create a new array full of the aforementioned items.

At the end of this file, you can see a demonstration of how you would encode an existing array of Codable items into memory through a PropertyListEncoder, dictionary key, and optional try block.

The main use case for this file will be running at the application initialization phase.

During this phase, we will examine a series of pre-existing items that we want to archive. If this array doesn’t exist, we can pre-populate an array of our items and then store it in memory to recall later.

Here is the end, hopefully useful to you. Thank you for watching.

Reference: 15 Quick Tips to Improve Your Swift Code

Share the news now

Source : Viblo