使用泛型使用 nib 加载 viewcontroller

Load viewcontroller with nib using generics

目前要使用相同的 nibname 加载 viewcontroller 我使用的代码如下

let recommendationVC : RecommendationVC = RecommendationVC(nibName: "RecommendationVC", bundle: nil)

我觉得指定 nibname 是不必要的,因为它与控制器名称相同。所以我决定使用泛型并使用泛型推断类型和笔尖名称

protocol NibIdentifiable {
    static var nibNameIdentifier: String { get }
}

// MARK: - Indentifies each storyboard from its classname.
extension NibIdentifiable where Self: UIViewController {
    static var nibNameIdentifier: String {
        return String(describing: self)
    }
}
extension UIViewController :NibIdentifiable
{

}

extension UIViewController {
    func instantiate<Controller: UIViewController>(_: Controller.Type) -> Controller where Controller: NibIdentifiable {

        guard let controller = Self(nibName:Controller.nibNameIdentifier,bundle:nil) as? Controller else {
            fatalError("Could not dequeue cell with identifier: \(Controller.nibNameIdentifier)")
        }

        return controller
    }
}

但是在尝试创建 VC 实例时,

 let recommendationVC :RecommendationVC = UIViewController.instantiate()

接收错误 无法推断通用参数 'Controller'

这种方法有什么问题?

class Rec : UIViewController  {
    let r : String = "1231231"
}

protocol NibIdentifiable {
    static var nibNameIdentifier: String { get }
}

// MARK: - Indentifies each storyboard from its classname.
extension NibIdentifiable where Self: UIViewController {
    static var nibNameIdentifier: String {
        return String(describing: self)
    }

    static func instantiate() -> Self {

        guard let controller = Self(nibName:Self.nibNameIdentifier,bundle:nil) as? Self else {
            fatalError("Could not dequeue cell with identifier: \(Self.nibNameIdentifier)")
        }

        return controller
    }

}

extension UIViewController : NibIdentifiable {
}

let x : Rec = Rec.instantiate()

这一定行得通。

在我的例子中,我使用了一些 Storyboardable 协议。并从特定的故事板启动控制器。

为 UIViewController 添加扩展

extension UIViewController {
    static func instantiateFromNib() -> Self {
        func instantiateFromNib<T: UIViewController>(_ viewType: T.Type) -> T {
            return T.init(nibName: String(describing: T.self), bundle: nil)
        }

        return instantiateFromNib(self)
    }
}

然后就这样使用

let myViewController = MyViewController.instantiateFromNib()