Cannot explicitly specialize a generic function

后端 未结 7 2308
时光取名叫无心
时光取名叫无心 2020-12-04 11:06

I have issue with following code:

func generic1(name : String){
}

func generic2(name : String){
     generic1(name)
}
         


        
7条回答
  •  抹茶落季
    2020-12-04 11:41

    The solution is taking the class type as parameter (like in Java)

    To let compiler know what type he is dealing with pass the class as argument

    extension UIViewController {
        func navigate(_ dump: ControllerType.Type, id: String, before: ((ControllerType) -> Void)?){
            let controller = self.storyboard?.instantiateViewController(withIdentifier: id) as! ControllerType
            before?(controller)
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }
    

    Call as:

    self.navigate(UserDetailsViewController.self, id: "UserDetailsViewController", before: {
            controller in
            controller.user = self.notification.sender
        })
    

提交回复
热议问题