Continuing the discussion from Contstructors and Init* Methods - No information on the wiki:
Is the information in the reply to this still correct ?
I have found myself adopting the Obj-C-like, explicit alloc.init pattern:
obj := MyClass.alloc.initWithFoo( foo );
I tried following the guidance in the above reference post, but code completion failed to offer a “withFoo” ‘constructor’ and if I ignored this and tried to compile with it anyway, it failed.
obj := new MyClass.withFoo( foo ); // Computer says "no"
I started getting creative and thought that perhaps “new ClassName” had been reduced to a more direct correspondence with ClassName.alloc, so tried restoring the initialiser method to its full name, but the compiler again rejected this because my initialiser method is not static (and rightly so):
obj := new MyClass.initWithFoo( foo ); // Computer says "no"
So thinking that perhaps the alloc equivalent was now “new ClassName()” my next attempt was to nudge the compiler into more certainly creating an instance before invoking the initialiser:
obj := new MyClass().initWithFoo( foo );
This compiles and indeed runs, but the initialiser doesn’t appear to function correctly.
In the concrete case the class is a custom view and the initialiser parameter is a CGRect that determines the frame of a new view instance. Using my long-hand alloc.init approach, this works perfectly. Using the above incantation the view is not initialised with the correct frame (though I have not investigated further to determine what frame it is getting).
obj := MyClass.alloc.initWithFoo( foo ); // works perfectly
obj := new MyClass().initWithFoo( foo ); // not right at all ?
Could I get a definitive answer on the right way to define and invoke constructors on classes in Nougat ?
I am perfectly happy to adopt the alloc.init pattern, but would appreciate knowing whether this is the officially supported mechanism or I am missing out on some syntactic sugar that I could otherwise take advantage of.