Q. What is the defer keyword in Swift?
A defer is a statement that is used for executing code just before transferring program control outside of the scope that the statement appears in.
In the above example, the output will be like this:
Image is going to delete…
Last name has removed…
- If multiple
deferstatements appear in the same scope, the order they appear is the reverse of the order they are executed.
The output will be this:
- defer don’t capture the reference or current value of a variable.
The output will be “Luther King” 2 times.
- We can’t break the defer execution or we can’t use return, continue, or break inside defer block.
Q. What is CustomStringConvertible?
It is a protocol to allow a type to provide custom textual descriptions of themselves. Every value in Swift is convertible.
Types that conform to the
CustomStringConvertible protocol can provide their own representation to be used when converting an instance to a string. The
String(describing:) initializer is the preferred way to convert an instance of any type to a string. If the passed instance conforms to
String(describing:) initializer and the
print(_:) function use the instance's custom
Accessing a type’s
descriptionproperty directly or using
CustomStringConvertibleas a generic constraint is discouraged.
Let’s see an example that how to use this protocol:
The output will be:
Person(name: “Alex John”, address: “California”, age: 25)
The output will be:
Alex John is 25 years old and living in California
Prefer way to print the custom description of an instance like:
Note: It is required to conform ‘CustomStringConvertible’ protocol to work with custom ‘description’ output.
Q. What is the difference between Array and Set?
- An array is a collection of ordered values and Set is a collection of unique values.
- An array is useful when order is important for you and Set is useful when you need to ensure that a value appears once in Set.
As we can see in the above example, we have initialized the array and set of string values.
In the output, we can notice that the order of Set is not the same as we initialized. Set is an unordered collection, so it may give you a different order of the result whenever you will access it.
Also, we have initialized Set with some duplicate elements like ‘John’. So in the output, we get unique values. Even if you will print the count of the Set collection with duplicate values, it will return the count as the number of unique elements.
Note: You can not access an element in a set using the ‘subscript’ operator. like namesWithSet. You have to iterate elements to access a particular element.
In the above example, we are trying to add more elements in the array and set. In array, we can use insert() and append() functions to add elements. But in Set, we can use only insert() function.
Note: The values stored in a Set need to conform to the ‘Hashable’ protocol to ensure that each value appears once in a Set.
When you insert an element in a Set, it returns a tuple (inserted, memberAfterInsert). We can ensure that the element has been inserted or not using the ‘inserted’ flag.
To remove an element from a Set, simply we can call remove(member:) function and its return the value that was removed. It's return nil if the value does not exist in the Set.
Power of Sets:
We can perform some other functions on sets also which are not available in Array, some useful functions are:
- union(): To combine two sets without duplicate values.
- intersection(): To get the common elements in two sets.
More questions will be adding soon in this article.
Comment your question in the comment box on which you want an answer.