How to automatically create an initializer for a Swift class?

IosSwiftStructModelInitialization

Ios Problem Overview


UPDATE: Use structs and not classes. Struct is better in many ways has got an initializer of its own.

This is my model class. Is it possible to create the init method automatically? Everytime I have to initialize all the variables one by one and it costs a lot of time.

class Profile {
        
    var id: String
    var name: String
    var image: String
        
    init(id: String, name: String, image: String) {
        self.id = id
        self.name = name
        self.image = image
    }
}

I want self.id = id and other variables to initialize automatically.

Ios Solutions


Solution 1 - Ios

Update As of Xcode 11.4

You can refactor (right-click mouse menu) to generate the memberwise initializer for class and struct.

Note that struct automatic initializers are internal. You may want to generate memberwise initializer when defining a module to make it public.

Right-click > Refactor > 'Generate Memberwise Initializer'

xcode generate memberwise initialization

For older Xcode

There are handy plugins:

https://github.com/rjoudrey/swift-init-generator https://github.com/Bouke/SwiftInitializerGenerator

Solution 2 - Ios

Given the following class (or for structs if temporarily change the keyword struct to class and after refactor set back to struct):

class MyClass {
    let myIntProperty: Int
    let myStringProperty: String
    let myOptionalStringProperty: String?
    let myForcedUnwrappedOptionalStringProperty: String!
}

Go to Xcode and:

  1. Double click the class name
  2. Right click
  3. Refactor
  4. Generate Member-wise Initializer

Above steps look like this:

enter image description here

Just a tiny second later, Xcode generates this initializer:

internal init(myIntProperty: Int, myStringProperty: String, myOptionalStringProperty: String?, myForcedUnwrappedOptionalStringProperty: String?) {
    self.myIntProperty = myIntProperty
    self.myStringProperty = myStringProperty
    self.myOptionalStringProperty = myOptionalStringProperty
    self.myForcedUnwrappedOptionalStringProperty = myForcedUnwrappedOptionalStringProperty
}

Solution 3 - Ios

No, there is no such feature for classes. But, if you design this as a struct, you get an memberwise initializer for free — assuming you don't define others initializers yourself.

For instance:

struct Point {
    var x: Float
    var y: Float
}
...
var p = Point(x: 1, y: 2)

From The Swift Programming Language book:

> Structure types automatically receive a memberwise initializer if they do not define any of their own custom initializers. Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that do not have default values. > > The memberwise initializer is a shorthand way to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBhavuk JainView Question on Stackoverflow
Solution 1 - IosMoris KramerView Answer on Stackoverflow
Solution 2 - IosJ. DoeView Answer on Stackoverflow
Solution 3 - IosPaulo MattosView Answer on Stackoverflow