我try 过用多种方法创建一个类的实例,使用字符串,但没有一种方法有效.
Below are pre-Swift 3 solutions I have tried that are not working
- Making class an objective-c class
@objc(customClass)
class customClass {
...
}
//Error here: cannot convert value of type 'AnyClass?' to expected argument type 'customClass'
let c: customClass = NSClassFromString("customClass")
- Specifying class using NSString value (both with and without using @objc attribute)
@objc(customClass)
class customClass {
...
}
//Error here: cannot convert value of type 'String' to expected argument type 'AnyClass' (aka 'AnyObject.Type')
var className = NSStringFromClass("customClass")
let c: customClass = NSClassFromString(className)
我做得不对,但还没有在网上找到任何解决方案.
How do I create an instance of a class using a string in Swift 3?