Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSClassFromString() always returns nil

The following code prints nil, despite ListCell is a valid class.

var lCellClass : AnyClass! = NSClassFromString("ListCell");

println(lCellClass);

The docs are saying that method returns

The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

I also tried to get NSClassFromString() of current viewcontroller which is LOADED but still get nil

What can be the problem ?

Update: Even trying to do this NSClassFromString("Array") I still get nil

like image 219
nsinvocation Avatar asked Jul 04 '14 08:07

nsinvocation


2 Answers

The NSClassFromString function does work for (pure and Objective-C-derived) swift classes, but only if you use the fully qualified name.

For example, in a module called MyApp with a pure swift class called Person:

let personClass: AnyClass? = NSClassFromString("MyApp.Person")

The module name is defined by the "Product Module Name" (PRODUCT_MODULE_NAME) build setting, typically the same name as your target (with some normalization applied to e.g. remove spaces).

This is atypical, but if the class you're loading is in the current module and you don't know the module name at compile-time e.g. because the code is compiled as part of multiple targets, you can look up the bundle name dynamically, which usually corresponds to the module name:

let moduleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
let personClass: AnyClass? = NSClassFromString(moduleName + "." + "Person")

I don't recommend this however unless you really don't know at compile-time.

See Using Swift Class Names with Objective-C APIs in Using Swift With Cocoa and Objective-C for more information.

like image 131
Jack Lawrence Avatar answered Nov 17 '22 23:11

Jack Lawrence


It's also possible to specify a name for the symbol in Objective-C which omits the namespace in objective C by using the @objc annotation. If you prefer to use the name without the module, just use the same class name:

@objc(Foobar)
class Foobar{

}

NSClassFromString("Foobar") will return the class Foobar.

like image 22
Osmund Avatar answered Nov 18 '22 01:11

Osmund