- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why is the Convenience Keyword Even Needed in Swift?
In Swift, you can create an additional initializer to provide default values for the properties. You can use the convenience keyword to add this functionality. Let's look at some examples of how to use a convenience initializer in the Swift language.
What is a convenience initializer in Swift?
In Swift, a secondary initializer in a class that provides extra or alternative ways to create an instance of that class is marked with the convenience keyword. The initialization procedure is streamlined and made simpler, which makes it easier for the developer to deal with the class. The designated initializer of the same class may be called the secondary initializer, reducing duplication of code and improving maintainability.
Here's an example of how to use the convenience initializer in Swift
Step 1 − In this example, we have a class named Person with a designated initializer that takes both a name and an age parameter. We also have a convenience initializer that only takes a name parameter and sets the age to a default value of 0.
Step 2 − The convenience initializer is marked with the convenience keyword. It calls the designated initializer using self.init and passes in the name parameter and a default value for age.
Step 3 − With this setup, we can create a Student instance with just a name using the convenience initializer, as we did with juile. The age is automatically set to the default value of 0. We can also create a Student instance with both a name and an age using the designated initializer as we did with alex.
Example
import Foundation class Student { let name: String let age: Int init(name: String, age: Int) { self.name = name self.age = age } convenience init(name: String) { self.init(name: name, age: 0) } } let alex = Student(name: "Alex Murphy", age: 30) let julie = Student(name: "Julie Martin") print("Alex age: \(alex.age)") print("Julie age: \(julie.age)")
Output
Alex age: 30 Julie age: 0
Here is another example of using a convenience initializer in Swift
Step 1 − In this example, we have a class Shape with a designated initializer that takes a name parameter. We also have a convenience initializer that sets the name to a default value of "Unknown Shape".
Step 2 − We also have a subclass Circle of Shape with a designated initializer that takes a name and a radius parameter. We also have a convenience initializer that takes only a radius parameter and sets the name to a default value of "Circle".
Step 3 − The convenience initializer in the Circle class is marked with the convenience keyword. It calls the designated initializer of the same class using self.init and passes in the default value for the name along with the radius parameter.
Step 4 − With this setup, we can create a Shape instance with just the default name using the convenience initializer, like we did with unknownShape. We can also create a Circle instance with just a radius using the convenience initializer as we did with the circle.
Step 5 − The name is automatically set to the default value of "Circle". We can also create a Circle instance with both a name and a radius using the designated initializer.
Example
import Foundation class Shape { let name: String init(name: String) { self.name = name } convenience init() { self.init(name: "Unknown Shape") } } class Circle: Shape { let radius: Double init(name: String, radius: Double) { self.radius = radius super.init(name: name) } convenience init(radius: Double) { self.init(name: "Circle", radius: radius) } } let unknownShape = Shape() let circle = Circle(radius: 5.0) print("Shape 1: \(unknownShape.name)") print("Shape 2: \(circle.name)")
Output
Shape 1: Unknown Shape Shape 2: Circle
The convenience initializer in Swift offers a number of advantages
Simplifies Initialization − The convenience initializer offers an additional or substitute method of creating a class instance, enhancing the developer's comfort and flexibility when working with the class. By offering default settings for some parameters or enabling some parameters to be removed entirely, it streamlines the initialization procedure.
Reduces Code Duplication − The convenience keyword reduces code duplication and enhances maintainability by enabling a secondary initializer to call the designated initializer of the same class. This ensures that any modifications made to the convenience initializer are automatically reflected in the specified initializer.
Provides Better Readability − The convenience initializer provides better code readability by making different initializers in order to simplify your codebase. It helps to understand the purpose of the initializer for developers.
Offers Greater Flexibility − You can create multiple convenience initializers according to your requirements. It helps to make classes more flexible and easy to write test cases.
Conclusion
Swift uses the convenience keyword to declare a class's convenience initializer, which offers an alternate method for initializing an instance. Convenience initializers can decrease code duplication, improve readability, and provide more flexibility while helping to simplify and speed up the initialization process. The convenience initializer makes it easier and more flexible for the developer to work with the class by offering default values for some arguments or enabling some parameters to be removed entirely.