convenience init

convenience init

So I learned a convenient and error-free way of assigning an image to an image view. Previously, I used a hardcoded string to access the image from the assets.

iv.imageView.image = UIImage(named: "someImage")        

However, I thought of accessing images using the dot syntax. My objective was to achieve the following syntax.

iv.imageView.image = UIImage(named: .someImage)        

The dot syntax is easy to use and human error-free. We don't need to use the hard-coded string explicitly, instead just use the dot to choose an image.

To achieve this, convenience init arrived as a saviour.

The convenience init is a special type of initializer in Swift that is used to create a convenience method to initialize an instance of a class or structure.

Here's what convenience init means and how it differs from a regular initializer:

  1. Convenience Initializer:

  • A convenience initializer is defined with the convenience keyword before the init keyword.
  • It provides an additional way to create an instance of a class or structure that may simplify the initialization process or provide default values.
  • Convenience initializers are secondary initializers and must ultimately call a designated initializer of the same class.
  • Convenience initializers cannot be overridden by subclasses.
  • They are often used to provide more convenient ways to initialize an object or to create alternative initialization paths.
  • Convenience initializers are optional and not required for every class or structure.

  1. Designated Initializer:

  • A designated initializer is the primary initializer of a class or structure.
  • It is responsible for initializing all properties of the class or structure and ensuring that the object is fully initialized.
  • Designated initializers are not prefixed with the convenience keyword.
  • They may have parameters and can call other designated initializers of the same class or superclass to delegate the initialization responsibilities.

In summary, a convenience init is a secondary initializer that provides a more convenient or alternative way to create an instance of a class or structure. It must call a designated initializer of the same class to ensure that all properties are properly initialized.


Here are a few steps one needs to take to choose the images using the dot syntax.

1) Creating an enum of type string that will hold the actual name of the image. The value is then mapped to its corresponding key.

For example

enum ImageName: String {

    case message = "Message"

    case lock = "Lock"

    case appLogo = "AppLogo"

}{        

2) After that, just extend the UIIMage and add the convenience init.


extension UIImage {


    convenience init(named imageName: ImageName) {

        self.init(named: imageName.rawValue)!
       }
}        

3) Now, Just access the images through dot syntax.

iv.imageView.image = UIImage(named: .message)        


Thats all.

To view or add a comment, sign in

More articles by Tehseen Ahmed

Others also viewed

Explore content categories