Skip to content
  • Home
  • About us
  • Contact
  • Privacy Policy
  • Disclaimer
  • Swift Online Compiler
iOS Interview Questions and Tutorials

iOS Interview Questions and Tutorials

Convenience initializers in Swift

Posted on November 26, 2023 By Sid No Comments on Convenience initializers in Swift
Convenience initializers in Swift
Convenience initializers in Swift:

In Swift, a convenience initializer is a secondary initializer that provides an additional way to create an instance of a class or struct. Convenience initializers are defined using the convenience keyword.

The primary initializer, often referred to as the “designated initializer,” is the main initializer that initializes all properties of a class or struct. Convenience initializers, on the other hand, provide an alternative way to create an instance by calling the designated initializer or another convenience initializer.

Here’s a simple example to illustrate the use of a convenience initializer:

class Shape {
    var width: Double
    var height: Double

    // Designated initializer
    init(width: Double, height: Double) {
        self.width = width
        self.height = height
    }

    // Convenience initializer for a square
    convenience init(side: Double) {
        // Calls the designated initializer with equal width and height
        self.init(width: side, height: side)
    }
}
// Creating an instance using the designated initializer
let rectangle = Shape(width: 4, height: 6)

// Creating an instance using the convenience initializer for a square
let square = Shape(side: 5)

In this example:

  • The Shape class has a designated initializer that initializes the width and height properties.
  • It also has a convenience initializer, init(side:), which allows you to create a square by calling the designated initializer with equal width and height.

Key points about convenience initializers:

  1. Must Call a Designated Initializer:
    • Convenience initializers must ultimately call a designated initializer of the same class. This ensures that all properties are properly initialized.
  2. Cannot Override Designated Initializers:
    • Convenience initializers cannot override designated initializers. They provide additional ways to initialize an instance but do not replace the primary initialization logic.
  3. Can Call Other Convenience Initializers:
    • Convenience initializers can call other convenience initializers within the same class.
  4. Typically Used for Additional Initialization Paths:
    • Convenience initializers are useful when you want to provide alternative ways to create an instance or introduce additional initialization logic.
  5. Commonly Used in Classes:
    • Convenience initializers are more commonly associated with classes, but you can also use them in structs.

When designing classes or structs, it’s a good practice to have a designated initializer that initializes all properties and provides a solid foundation for the instance’s state. Convenience initializers then complement the designated initializer by offering flexibility in how instances can be created.

Blog Tags:Convenience initializers in Swift

Post navigation

Previous Post: Failable initializers in Swift
Next Post: Closures in Swift: Brief Explanation with Code Examples

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Asked iOS Interview Questions

  • Top iOS Interview Questions and Answers

Categories

  • Associated Types(7)
  • Blog
  • Dictionary in Swift(20)
  • Initializers
  • Property Wrapper
  • Singleton in Swift
  • User Defaults(4)
  • XCode 15 Errors

Recent Comments

  1. Sid on Cycle inside MyApp; building could produce unreliable results
  2. Anominous on Cycle inside MyApp; building could produce unreliable results
  3. Aisha on @objc Attribute in Swift

Recent Posts

  • Enums in Swift: Brief Explanation with Code Examples
  • Higher-Order Functions in Swift: Brief Explanation with Code Examples
  • Mutability in Structs and Classes in Swift
  • Delegate Pattern in Swift
  • resueIdentifier in Swift

DSA in Swift

  • 2D Array in Swift: Interview Questions

Copyright © 2025 iOS Interview Questions and Tutorials.

Powered by PressBook WordPress theme