Swift - 在分机中获取 class 名称?
Swift - get class name in extension?
我正在尝试从扩展程序的 class 方法中获取 class 名称。
这可能吗?我最终以 "DRHT" 作为 class
的名称
扩展
extension UIViewController {
public class func instanceFromStoryboard(storyboardIdentifier: String = "Main") -> UIViewController {
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
let controllerIdentifier = NSStringFromClass(self)
return storyboard.instantiateViewControllerWithIdentifier(controllerIdentifier) as UIViewController
}
}
用法
let homeViewController = HomeViewController.instanceFromStoryboard()
我最终采用了不同的方式来使其更加类型安全并消除转换。
extension UIStoryboard {
public class func instantiateViewController <T: UIViewController>(type: T.Type, storyboardIdentifier: String = "Main") -> T {
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
return storyboard.instantiateViewController(type)
}
public func instantiateViewController <T: UIViewController>(type: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(type)) as! T
}
}
extension UIViewController {
public class func instantiateFromStoryboard(storyboardName: String = "Main") -> Self {
return UIStoryboard.instantiateViewController(self, storyboardIdentifier: storyboardName)
}
}
用法
let vc = UIStoryboard.instantiateViewController(HomeViewController.self)
或
let vc = UIStoryboard.instantiateViewController(HomeViewController.self, storyboardIdentifier: "Storybaord_Name")
或
let vc = storyboard.instantiateViewController(HomeViewController.self)
或
let vc = HomeViewController.instantiateViewController()
感谢MartinR and his ,我知道答案了:
extension UIViewController
{
class func instantiateFromStoryboard() -> Self
{
return instantiateFromStoryboardHelper(self, storyboardName: "Main")
}
class func instantiateFromStoryboard(storyboardName: String) -> Self
{
return instantiateFromStoryboardHelper(self, storyboardName: storyboardName)
}
private class func instantiateFromStoryboardHelper<T>(type: T.Type, storyboardName: String) -> T
{
var storyboardId = ""
let components = "\(type.dynamicType)".componentsSeparatedByString(".")
if components.count > 1
{
storyboardId = components[1]
}
let storyboad = UIStoryboard(name: storyboardName, bundle: nil)
let controller = storyboad.instantiateViewControllerWithIdentifier(storyboardId) as! T
return controller
}
}
用法:
let vc = HomeViewController.instantiateFromStoryboard()
我正在尝试从扩展程序的 class 方法中获取 class 名称。 这可能吗?我最终以 "DRHT" 作为 class
的名称扩展
extension UIViewController {
public class func instanceFromStoryboard(storyboardIdentifier: String = "Main") -> UIViewController {
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
let controllerIdentifier = NSStringFromClass(self)
return storyboard.instantiateViewControllerWithIdentifier(controllerIdentifier) as UIViewController
}
}
用法
let homeViewController = HomeViewController.instanceFromStoryboard()
我最终采用了不同的方式来使其更加类型安全并消除转换。
extension UIStoryboard {
public class func instantiateViewController <T: UIViewController>(type: T.Type, storyboardIdentifier: String = "Main") -> T {
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
return storyboard.instantiateViewController(type)
}
public func instantiateViewController <T: UIViewController>(type: T.Type) -> T {
return instantiateViewControllerWithIdentifier(String(type)) as! T
}
}
extension UIViewController {
public class func instantiateFromStoryboard(storyboardName: String = "Main") -> Self {
return UIStoryboard.instantiateViewController(self, storyboardIdentifier: storyboardName)
}
}
用法
let vc = UIStoryboard.instantiateViewController(HomeViewController.self)
或
let vc = UIStoryboard.instantiateViewController(HomeViewController.self, storyboardIdentifier: "Storybaord_Name")
或
let vc = storyboard.instantiateViewController(HomeViewController.self)
或
let vc = HomeViewController.instantiateViewController()
感谢MartinR and his
extension UIViewController
{
class func instantiateFromStoryboard() -> Self
{
return instantiateFromStoryboardHelper(self, storyboardName: "Main")
}
class func instantiateFromStoryboard(storyboardName: String) -> Self
{
return instantiateFromStoryboardHelper(self, storyboardName: storyboardName)
}
private class func instantiateFromStoryboardHelper<T>(type: T.Type, storyboardName: String) -> T
{
var storyboardId = ""
let components = "\(type.dynamicType)".componentsSeparatedByString(".")
if components.count > 1
{
storyboardId = components[1]
}
let storyboad = UIStoryboard(name: storyboardName, bundle: nil)
let controller = storyboad.instantiateViewControllerWithIdentifier(storyboardId) as! T
return controller
}
}
用法:
let vc = HomeViewController.instantiateFromStoryboard()