Passing optional callback into Swift function

前端 未结 3 786
野性不改
野性不改 2021-02-01 01:06

I\'m learning Swift lang, but I cannot pass optional callback argument into function:

func dismiss(completion: () -> Void) {
    if (completion) {
        ret         


        
3条回答
  •  别那么骄傲
    2021-02-01 01:39

    Just adding to Martin R's answer above..

    The callback can be optional, instead of implicit parameter (with exclamation mark), use the optional operator.

    func dismiss(completion: (() -> Void)?) {
        if completion != nil {
            return self.dismissViewControllerAnimated(true, completion: completion!)
        }
    
        self.dismissModalViewControllerAnimated(true)
    }
    

提交回复
热议问题