<div dir="ltr"><div><div>Hello everyone,</div><div>In objc there is quite common pattern when we use a base class with a type property and concrete subclasses where type is uniquely identifying the subclass.</div><div>We can then safely put subclasses objects into an array and then safely downcast them when needed. </div><div>This kind of behaviour is commonly used in data sources implementations.</div><div><br></div><div>Like this:</div><div><br></div><div>typedef NS_ENUM(NSUInteger, CellDecriptorType) {</div><div>    CellDecriptorTypeUnknown,</div><div>    CellDecriptorType1,</div><div>    CellDecriptorType2</div><div>};</div><div><br></div><div>@interface BaseCellDescriptor : NSObject</div><div><br></div><div>@property (nonatomic, readonly) CellDecriptorType type;</div><div>@property (nonatomic, strong) id value;</div><div><br></div><div>@end</div><div><br></div><div><br></div><div>@interface CellDescriptor1 : BaseCellDescriptor</div><div>@end</div><div><br></div><div>@implementation CellDescriptor1</div><div><br></div><div>- (CellDecriptorType)type {</div><div>    return CellDecriptorType1;</div><div>}</div><div><br></div><div>- (NSString *)value {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>return @&quot;string value&quot;;</div><div>}</div><div><br></div><div>@end</div><div><br></div><div><br></div><div>@interface CellDescriptor2 : BaseCellDescriptor</div><div>@end</div><div><br></div><div>@implementation CellDescriptor2</div><div><br></div><div>- (CellDecriptorType)type {</div><div>    return CellDecriptorType2;</div><div>}</div><div><br></div><div>- (NSNumber *)value {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>return @42;</div><div>}</div><div><br></div><div>@end</div><div><br></div><div>And somewhere later we do:</div><div><br></div><div>- (void)doWorkWithCellDescriptors:(NSArray&lt;BaseCellDescriptor *&gt; *)descriptors {</div><div>    for (BaseCellDescriptor *descriptor in descriptors) {</div><div>        CellDecriptorType type = descriptor.type;</div><div>        switch(type) {</div><div>            case CellDecriptorType1: {</div><div>                CellDescriptor1 *aDescriptor = (CellDescriptor1 *)descriptor;</div><div>                NSString *value = aDescriptor.value;</div><div>                // Do something with value</div><div>                break;</div><div>            }</div><div>            case CellDecriptorType2: {</div><div>                CellDescriptor2 *aDescriptor = (CellDescriptor2 *)descriptor;</div><div>                NSNumber *value = aDescriptor.value;</div><div>                // Do something with value</div><div>                break;</div><div>            }</div><div>            case CellDecriptorTypeUnknown:</div><div>            default: {</div><div>                // Handle error</div><div>                break;</div><div>            }</div><div>        }</div><div>    }</div><div>} </div><div><br></div><div><br></div><div>I want to implement it swifty way. So the questions are:</div><div>0. Is it a bad practice to use this pattern and how we can avoid it?</div><div>1. Is it possible to avoid inheritance here and only use generic protocols and how?</div><div>2. Is it possible to avoid downcasting if using this pattern in swift?</div><div><br></div><div><br></div><div>I&#39;ve found the solution that seems to be a good example in this project <a href="https://github.com/xmartlabs/Eureka">https://github.com/xmartlabs/Eureka</a>.</div><div>They maintain both inheritance hierarchy and protocol hierarchy.</div><div><br></div><div>3. Is it a good practice to implement such tasks like in Eureka project, what are the pros and cons of it?</div><div><br></div><div>Thanks for your time!</div></div></div>