Generate custom init method automatically Generate custom init method automatically xcode xcode

Generate custom init method automatically


New Xcode (after Xcode 10) support this issue for class (not for struct).

Steps:

  1. right click on class name
  2. click "refactor"
  3. click "Generate Memberwise Initializer"

enter image description here

As for struct. You can make it class first, and change it back to struct after you get auto-gen init.


While there's still no way to do this automatically (without installing a plugin), there's a neat trick to convert a list of property declarations to assignments using multiple cursors:

  • Copy the list of properties and paste it into your constructor
  • Use Shift+Ctrl + click to insert multiple cursors (Shift+Ctrl+/ work as well)
  • Edit with multiple cursors to assign values

multi cursor

Credits go to Paul Hudson: Xcode in 20 Seconds: Multiple cursors


There is no native way of doing this, however you can install XCode extensions that will add support for this.

See this following extension as this will provide the feature you are after (Swift version).

https://github.com/Bouke/SwiftInitializerGenerator