Generate custom init method automatically

Javier Roberto picture Javier Roberto · Apr 29, 2014 · Viewed 7.3k times · Source

Is it possible to generate custom init method automatically from Xcode, as Android Studio does for android? I mean, if I declare some properties in .h, for example: int a; int b;

So, I would like to create automatically a init method as:

- (id)initWithA:(int) aInner andB:(int) bInner
{
     a = aInner;
     b = bInner;
}

Answer

Wu Yuan Chun picture Wu Yuan Chun · Dec 18, 2019

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.