Passing optional callback into Swift function

前端 未结 3 785
野性不改
野性不改 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:40

    It's better to add = nil in the callback declaration, to avoid passing nil while calling it:

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

    And you can call your function like this : dismiss()

提交回复
热议问题