OC中可以使用NSClassFromString将字符串直接转换为类名,在Swift中利用NSClassFromString不出意外结果都为nil,因为Swift中根据字符串转换的方法需要加上YourAppName,格式为”YouAPPName.类名”
写了一个类目,具体代码如下:
import Foundation
import UIKit
extension NSObject {
func swiftClassFromString(className: String) -> UIViewController! {
// get the project name
if let appName: String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as!