swift 通过 string 创建 object

1
2
3
4
5
6
7
8
9
10
11
class IndexViewController: UIViewController{}
 
let className = "IndexViewController"
 
let bundlePath = NSBundle.mainBundle().bundlePath
let bundleFullName = bundlePath.componentsSeparatedByString("/").last
let bundleName = bundleFullName?.componentsSeparatedByString(".").first
let clazz = NSClassFromString(bundleName! + "." + className)! as! UIViewController.Type
 
let object = clazz.init()
// let object1 = Index() 普通方法