Skip to content

Instantly share code, notes, and snippets.

@xenophy
Last active September 19, 2015 23:50
Show Gist options
  • Save xenophy/9a912ec6237be9b8fe06 to your computer and use it in GitHub Desktop.
Save xenophy/9a912ec6237be9b8fe06 to your computer and use it in GitHub Desktop.
Create dynamic class instance from String in Swift 2.0
// 実行アプリ名前空間取得
let namespace = NSStringFromClass(self.dynamicType).componentsSeparatedByString(".")[0]
// 生成クラス名取得
let clsName = namespace + ".MyViewController"
// ViewController生成
var cls:UIViewController.Type = NSClassFromString(clsName) as! UIViewController.Type
var vc:UIViewController = (cls.init() as UIViewController)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment