Swift language NSClassFromString

How to achieve reflection in Swift Language?

How can I instantiate a class

[[NSClassFromString(@"Foo") alloc] init];

Solution 1:

You must put @objc(SwiftClassName) above your swift class.
Like:

@objc(SubClass)
class SubClass: SuperClass {...}

Solution 2:

This is the way I init derived UIViewController by class name

var className = "YourAppName.TestViewController"
let aClass = NSClassFromString(className) as! UIViewController.Type
let viewController = aClass()

More information is here

In iOS 9

var className = "YourAppName.TestViewController"
let aClass = NSClassFromString(className) as! UIViewController.Type
let viewController = aClass.init()

Solution 3:

Less hacky solution here: https://stackoverflow.com/a/32265287/308315

Note that Swift classes are namespaced now so instead of "MyViewController" it'd be "AppName.MyViewController"


Deprecated since XCode6-beta 6/7

Solution developed using XCode6-beta 3

Thanks to the answer of Edwin Vermeer I was able to build something to instantiate Swift classes into an Obj-C class by doing this:

// swift file
// extend the NSObject class
extension NSObject {
    // create a static method to get a swift class for a string name
    class func swiftClassFromString(className: String) -> AnyClass! {
        // get the project name
        if  var appName: String? = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as String? {
            // generate the full name of your class (take a look into your "YourProject-swift.h" file)
            let classStringName = "_TtC\(appName!.utf16count)\(appName)\(countElements(className))\(className)"
            // return the class!
            return NSClassFromString(classStringName)
        }
        return nil;
    }
}

// obj-c file
#import "YourProject-Swift.h"

- (void)aMethod {
    Class class = NSClassFromString(key);
    if (!class)
        class = [NSObject swiftClassFromString:(key)];
    // do something with the class
}

EDIT

You can also do it in pure obj-c:

- (Class)swiftClassFromString:(NSString *)className {
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    NSString *classStringName = [NSString stringWithFormat:@"_TtC%d%@%d%@", appName.length, appName, className.length, className];
    return NSClassFromString(classStringName);
}

I hope this will help somebody !

Solution 4:

UPDATE: Starting with beta 6 NSStringFromClass will return your bundle name plus class name separated by a dot. So it will be something like MyApp.MyClass

Swift classes will have a constructed internal name that is build up of the following parts:

  • It will start with _TtC,
  • followed by a number that is the length of your application name,
  • followed by your application name,
  • folowed by a number that is the length of your class name,
  • followed by your class name.

So your class name will be something like _TtC5MyApp7MyClass

You can get this name as a string by executing:

var classString = NSStringFromClass(self.dynamicType)

Update In Swift 3 this has changed to:

var classString = NSStringFromClass(type(of: self))

Using that string, you can create an instance of your Swift class by executing:

var anyobjectype : AnyObject.Type = NSClassFromString(classString)
var nsobjectype : NSObject.Type = anyobjectype as NSObject.Type
var rec: AnyObject = nsobjectype()

Solution 5:

It's almost the same

func NSClassFromString(_ aClassName: String!) -> AnyClass!

Check this doc:

https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/#//apple_ref/c/func/NSClassFromString