is it possible to create a generic closure in Swift?

后端 未结 4 1289
一生所求
一生所求 2020-12-30 19:49
func myfunc(i:T) -> T {
    return i
}

is it possible to make this generic function a closure?

let myfunc = {          


        
4条回答
  •  借酒劲吻你
    2020-12-30 20:40

    I have found some alternative way , you can use Anyobject in your closure and pass any values to your method .

    typealias genericCompletion = ((Bool,T,String) -> Void)
    struct Student {
        var name:String = "Kishore"
        var age : String = "125"
    }
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.createAGenericReturn { (success, object, message) in
    
            }
    
            self.createStructGeneric { (success, student, message) in
    
            }
    
        }
    
    
        func createAGenericReturn(callback:@escaping(genericCompletion)){
            callback(true,434.433 as AnyObject,"kishoreTest")
        }
    
        func createStructGeneric(callback:@escaping(genericCompletion)){
            callback(true,Student.init() as AnyObject,"kishoreTest")
        }
    
    }
    

    Here you can see I mentioned Generic as Anyobject typealias genericCompletion = ((Bool,T,String) -> Void) , So you can pass any values to it .

提交回复
热议问题