jeudi 6 août 2015

How to avoid rewriting Initializers in Swift?

With Objective-C I am initializing my classes like that:

SomeClass *anInstance = [SomeClass new]

anInstance.importantInt = 4;
anInstance.importantString = @"Vip";
anInstance.importantDelegate = self;

[anInstance initialize];

I am doing it like that to avoid rewriting my initializers or to avoid having multiple initializers with duplicated code during ongoing development of a class. I am calling -(void)initialize to do the setup of the instance.

This way I can use dot-notation to assign properties and simply add a new line if I need to add properties to the class later on.

To avoid adding -(void)initialize to every header I am doing this using an extension of NSObject or my own superclass or with protocols.

As I am now starting to work with swift I was asking myself if it still makes sense to do it in this "personal design pattern". Are there concepts in swift that would allow me to easily add properties without rewriting initializers to assign external values?

I already read through the swift guide from Apple an took a deeper look into initialization of classes/structures but I really do not recognize any situation where these swift benefits could help me in my situation.

Aucun commentaire:

Enregistrer un commentaire