Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 323 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NSClassFromString() always returns nil

#1
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`
Reply

#2
For example:

Just using UIViewController as an example,

```
func getVc(from stringName: String) -> UIViewController? {
let appName = Bundle.main.infoDictionary!["CFBundleName"] as! String
guard let vc = NSClassFromString(appName + "." + stringName) as? UIViewController.Type else {
return nil
}
return vc.init()
}
```

**If your App name contains "-" or number, just replace them with "_"**
Reply

#3
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](

[To see links please register here]

) for more information.
Reply

#4
This may be problem that your class have not extend from NSObject. As by default swift does not have superclass.
Reply

#5
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through