Using Runtime in Swift without Objective-C

Hi, everyone
It's me again. I have a question here:
As we all know, we can call or reference functions in Runtime by using Objective-C.
Example:
In class 'Feature.m',

 - (UIViewController *)show_Feature_ViewController
     {
         ViewController *viewController = [[ViewController alloc] init];
         return viewController;
     }

In somewhere, it can be handled as below:

-(UIViewController *)show{
    Class targetClass = NSClassFromString("Feature");//"Feature" maybe from somewhere who deliver the name as param
            target = [[targetClass alloc] init];
    SEL action = NSSelectorFromString("show_Feature_ViewController");
    if ([target respondsToSelector:action]) {
          return  [target performSelector:action withObject:nil];
        } 
    }

So, my question here is how can i do that in Swift?
Thank you!

I don’t have any immediate answers for you, but I do have some suggestions and questions:

  • Try formatting your code as code blocks (you can use triple backticks to delimit). That’ll make it easier to read.

  • Can you explain your motivations here? The Swift and Objective-C runtimes are quite different, and it would help to have a better understanding of your big picture goals.

  • Are you targeting Apple platforms? Or do you need a solution that works on non-Apple platforms?

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Hello,
Thank you for your suggestions here. It's awesome to format my code. Forgive me as a newbie.
BTW, my motivations here is:
In Objective-C, we can transform a string to method name, AKA action name, also can transform a string to Class.
So my questions here is that any solutions to do these in Swift without using @objc or inherit by NSObject?
Thank you again.

So my questions here is that any solutions to do these in Swift
without using @objc or inherit by NSObject?

The no-Obj-C solutions in this space are rather limited. However, my experience is that I don’t miss them very much. When working with Swift I try to lean on the more advanced Swift features to create solutions that are both compact and type safe. If you can expand on your requirements a bit, I can certainly offer some suggestions.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple