Class as function parameter in Swift

In my app there are many classes for different ViewControllers.
I am searching way to give class name as function parameter so i can use it in function.


  • How to save image
  • iPhone Data Usage Tracking/Monitoring
  • startMonitoringSignificantLocationChanges not working in swift
  • Delete files from NSCachesDirectory programmatically
  • Creating a UITableViewCell programmatically in Swift
  • uipageviewcontroller class crashing on setting more than one view controllers
  • func myFunc (var1: String, var2 : Int, var3 : myClass) {
     var example = myClass(); = var1;
     example.age = var2;

    i saw this thread Class conforming to protocol as function parameter in Swift but i didn’t understand how can i solve my problem with it.

    Solutions Collect From Internet About “Class as function parameter in Swift”

    You can do this without the need to pass an instance of your view controller:

    import Cocoa
    protocol MyViewControllerType : class {
        var name : String { get set }
        var age : Int { get set }
    class FirstViewController : NSViewController, MyViewControllerType {
        var name = ""
        var age = 0
    class SecondViewController : NSViewController, MyViewControllerType {
        var name = ""
        var age = 0
    func myFunc<VC : NSViewController where VC : MyViewControllerType>(viewControllerType: VC.Type, name: String, age: Int) -> VC {
        let viewController = VC() = name
        viewController.age = age
        return viewController
    let first = myFunc(FirstViewController.self, name: "Bob", age: 20)
    let second = myFunc(SecondViewController.self, name: "Paul", age: 30)