In Obj-C, how can I make a switch statement that will evaluate classes, not just numbers?

in Obj-C, a switch can only evaluate numbers. I would like to use it to compare feature classes, for example:

switch (currentSubViewController.class) { case UITableViewController.class : <do stuff> break; case UICollectionViewController.class : <do stuff> break; } 

Is there any way to achieve this? I would really like to use the switch, because it makes it easy to read for different cases, and I can add more cases at any time in the future. Any ideas?

+6
source share
5 answers

As described in this post, you better use the Liskov Substitution Principle and put <do stuff> in the actual class, and then call the method shared by the superclass that all these classes inherit (or the protocol in Obj-C if you plan to use this logic for different classes).

This encapsulates all the logic in each implementation / subclass without a higher level class that needs to worry about every possible implementation you might have. This improves maintainability and is more common in object-oriented programming, unlike the simple old procedural programming.

+9
source

The switch only works with integer types. You need a big if-else block.

If you really want to force the switch , then you can work out something using a fixed array of classes and justify turning off the position of the index of the class you are checking. But for readability in case statements, you will need to define a set of constants representing the index position for each class. This is a lot of work and support code to avoid and if-else block.

+1
source

It's impossible. You should use an if-elseif like this

 if ([currentSubViewController isMembefOrClass:[UITableViewController class]]) { <do stuff> } else if ([currentSubViewController isMemberOfClass:[UICollectionViewController class]]) { } 

Note that I am using isMemberOfClass: to model what the switch-case will do. But in most cases you want to use isKindOfClass:

0
source

You can use the dictionary.

 ((void(^)())@{ NSStringFromClass([UITableViewController class]) : [^{ // stuff } copy], NSStringFromClass([UICollectionViewController class]) : [^{ // stuff } copy] }[NSStringFromClass([currentSubViewController class])])(); 

NOTE. It is just for fun. Please do not take this as a serious offer.

Seriously use @AramKocharyan's answer.

0
source

One way is to create a data structure matching the classes you like in enum . This will be a lot of boiler plate code.

Unfortunately, Objective-C does not have object-oriented switch conditional languages ​​such as Ruby. It has only a C switch . A case in a C switch requires an integer constant, and this prevents more dynamic approaches using C switch . The enum C type also works with constants. This means that none of them can be generated at runtime, but can be at compile time.

Enumeration operators and C switches work well together, but not so hot for objects.

So, you might be better off making if-else compound expressions to do your comparisons here.

0
source

Source: https://habr.com/ru/post/945386/


All Articles