Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift language NSClassFromString

How to achieve reflection in Swift Language?

How can I instantiate a class

[[NSClassFromString(@"Foo") alloc] init];
like image 977
Selvin Avatar asked Jun 04 '14 06:06

Selvin


6 Answers

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

@objc(SubClass)
class SubClass: SuperClass {...}
like image 54
klaus Avatar answered Nov 19 '22 21:11

klaus


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()
like image 58
Rigel Chen Avatar answered Nov 19 '22 21:11

Rigel Chen


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 !

like image 37
Kevin Delord Avatar answered Nov 19 '22 22:11

Kevin Delord


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()
like image 27
Edwin Vermeer Avatar answered Nov 19 '22 22:11

Edwin Vermeer


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

like image 11
gabuh Avatar answered Nov 19 '22 20:11

gabuh


I was able to instantiate an object dynamically

var clazz: NSObject.Type = TestObject.self
var instance : NSObject = clazz()

if let testObject = instance as? TestObject {
    println("yes!")
}

I haven't found a way to create AnyClass from a String (without using Obj-C). I think they don't want you to do that because it basically breaks the type system.

like image 9
Sulthan Avatar answered Nov 19 '22 20:11

Sulthan