5 quick tips to help you code Swift more efficiently

Tram Ho

1.allSatisfy – checks if the collection satisfies the condition

Since Swift 4.2 we have the allSatisfy () method for collections (array, set, …). This function accepts a condition, and returns true if all of the items in that collection meet this condition.

For example, we have the following array to hold the test scores of a class:

Assuming 5 points are sufficient to pass the subject, we can check if all students in the class passed by:

2. Public getter, private setter

Access control is a great feature of swift. Maybe you also use access controls like public and private for class properties to control code elsewhere to access these properties, and make the purpose of our code clearer. But did you know that we can use two types of access controls at the same time. See the following example:

We have a struct named Bank:

Then we instantiate an instance of this struct:

We do not use any access control with the property address , which means that code everywhere can read and change the address value of richBank . If we use private for address, the code elsewhere cannot change or read its value.

However, there will be times when you want code elsewhere to be able to read the address value, but only internally richBank can change the value of this property. Swift provides a very easy way to do this: public private (set) .

Using this syntax, a property can be read anywhere, but can only be changed internally. Specifically, in our example, anyone can read the richBank address, but only richBank itself can change its address.

3. Static vs class properties

Class properties in Swift can be created using one of two keywords: static and class .

Both make the property a common property in every instance of the class, but the static keyword is final , ie a property created with a static keyword cannot override the subclass.

For example, we could create a Building class that has a class property that holds the address, and a static property that holds the names of the people who own this Building:

Because address is a class property, it can be changed by the subsclass of the Building. In contrast, owners are a static property that cannot be changed in subclasses.

4. == vs. ===

In Swift there is a protocol named Equatable . When a type applies this protocol, we can use operator “==” to check if the 2 values ​​of that type are the same. For example:

When you create a new type, you just need to apply the Equatable protocol to use ==. However, if you use a class and want to compare two instances of the class against each other, you have one more option.

Because a class instance is just a reference to that class’s address in memory, you can use operator === to check if two instances of the same class point to the same address.

You can see that the above class does not need to apply the Equatable protocol but is still able to compare the instances. === is also known as the identity operator.

5. Create default value for protocol property using extension

We all know we can create a default implementation for a method of the protocol using an extension . However, we can also use the extension to assign default values ​​to protocol properties .

For example, we could create a protocol called Fadeable that will obscure the view for a few seconds:

Instead of having each type apply this protocol add values ​​for the fadeSpeed and implement the fadeOut () method, we can set default values ​​for both in the extension:

Then, we can let our subclass apply this protocol without having to rewrite the defaults listed above:

Above are 5 quick and useful tips in Swift. Hope my articles can help you code more effectively.

Share the news now

Source : Viblo