How? function as param with a default??

I’m trying to use a function as a parameter in (duh) another function.

The param function is a callback.

I’ve got another function in the same class which matches the required format:

I want to use deadCallback as a default for the param so the function myFunc doesn’t *require* a callback param.

 

If I leave the code: = self.deadCallback out the app compiles with no problem.

If I include it I get a compile time error:
“value of type (myClass) -> () -> (myClass) has no member deadCallback”

 

What am I doing wrong?
Can I do this?
If so, how?

 

class myClass {

    

     func myFunc(_ url: String, callback: @escaping (_ resp: String) -> () = self.deadCallback) {

          //do something that might make a string

          var myStr? = “blah blah blah” //this may be nil which is perfectly acceptable

          if(myStr != nil) {

               callback(myStr!)

          }

     }

    

     func deadCallback(_ resp: String) {

          return

     }

 

}

Powered by WPeMatico

About

You may also like...

Comments are closed.