Options with extra shutters in fast

I use additional locks, but cannot find a way to pass the parameter. I searched everywhere, tried all the offers, but could not get it to work.

My code is:

func DoAlert(title: String , message: String , actions: String , sender: AnyObject? , Ctlr : UIViewController , SegueString: String? , YesClosure: ()->() = {} , NoClosure: ()->() = {} , StartClosure: ()->() = {} , EndClosure: ()->() = {} ) { if (actions.rangeOfString("Ok") != nil { alert.addAction(UIAlertAction(title: "OK", style: .Default ) { action -> Void in EndClosure() })} } // end function 

I want to add a closure for Ok, where the "self" parameter is needed.

Something like below:

  // add to func doAlert: , OkClosure: (AnyObject)->() = {} // add to action Ok (before the EndClosure: OkClosure(sender!) 

Error on first line: AnyObject is not a subtype ()

If I left AnyObject from the first line, Error on receipt: Unable to convert expression type 'AnyObject' to type '() => ()'

All other tests give me similar "Tuple" errors. How to encode parameter passing in optional closures in my code?

+6
source share
2 answers

First, to use closure as an argument to a function, you should declare them like this:

 func myFunc(closure: (Int) -> Void) { // Now I can call closure like so: let myInt = 10 closure(myInt) } 

(As pointed out by @Airspeed Velocity, parentheses around Int are not strictly required because there is only one argument. Do you include them only in your personal setting)

Secondly, you can modify the previous function to enable the optional closure, as follows: (Note the ? And brackets around the closure, indicating that the closure is an optional and not a return type)

 func myFunc(closure: ((Int) -> Void)?) { // Now when calling the closure you need to make sure it not nil. // For example: closure?(10) } 

Thirdly, to add a default value of nil, which is similar to what you are trying to do with = {} at the end of YesClosure: ()->() = {} , you can do:

 func myFunc(closure: ((Int) -> Void)? = nil) { // Still need to make sure it not nil. if let c = closure { c(10) } } 

Finally, as a note, you can set the names of the closing arguments, which will make it easier to determine what you pass when closing when called. For instance:

(Note - brackets around value: Int are required here)

 func myFunc(closure: ((value: Int) -> Void)) { closure(value: 10) } 

Finally, typealias can be used. According to the documentation:

A type alias declaration introduces a named alias of an existing type into your program.

Here is an example of how to use it with closure:

 typealias MyClosureType = () -> Void func myFunc(closure: MyClosureType) { closure() } 

Hope this helps!

+17
source

I think I found it. I cannot use parameters in closing when I call func. In the function itself, I need to determine the parameters used (closure: (sender: AnyObject) → Void), make sure the variables are defined (or provided as a separate parameter) and add them to the closure call.

 @IBAction func buttonPressed(sender: AnyObject) { myFunc (sender, doClosure) } func myFunc(sender: AnyObject, closure: (sender: AnyObject) -> Void) { // Now I can call closure like so: closure (sender: sender) } func doClosure(sender: AnyObject) { println("sender = \(sender)") } 
0
source

Source: https://habr.com/ru/post/984918/


All Articles