title: Swift Passing Closure With Params
body: <p>Currently I'm passing in a closure as a property on an object that accepts no params and has no return value as follows:</p>
  
  <pre><code>class MyClass {
      var myClosureProperty: (() -&gt; ())? {
              didSet {
                  doSomeActionWhenClosureIsSet()
              }
      }
  }
  
  var instanceOfMyClass = MyClass()
  instanceOfMyClass.myClosureProperty = {
      // do some things here...
  }
  </code></pre>
  
  <p>and so far this is working great. I want to be able to pass in a parameter when setting this closure to be used inside the instance of MyClass. I'm looking for SOMETHING like below, although the syntax I'm sure is incorrect:</p>
  
  <pre><code>class MyClass {
      var myClosureProperty: ((newString: String) -&gt; ())? {
              didSet {
                  doSomeActionWhenClosureIsSet(newString)
              }
      }
  
      func doSomeActionWhenClosureIsSet(stringParam: String) -&gt; () {
          // create a button with the stringParam title...
      }
  }
  
  var instanceOfMyClass = MyClass()
  instanceOfMyClass.myClosureProperty = {("Action")
      exampleFunction()
  }
  </code></pre>
  
  <p>How would I go about passing in a parameter to this closure that can be used inside of MyClass - i.e. a value that can be used inside the didSet portion of the property itself as in the second example?</p>
  
  <p>EDIT: Here's what ended up working for me:</p>
  
  <pre><code>class MyClass {
          var myClosurePropertyWithStringAndAction: (buttonName: String, closure: (() -&gt; ()))? {
              didSet {
                    let buttonTitle = myClosurePropertyWithStringAndAction!.buttonName
                    createButtonWithButtonTitle(buttonTitle)
              }
           }
  
          func createButtonWithButtonTitle(buttonTitle: String) -&gt; () {
               // here I create a button with the buttonTitle as the title and set
               // handleButtonPressed as the action on the button
          }
  
          func handleButtonPressed() {
              self.myClosurePropertyWithStringAndAction?.closure()
          }
      }
  }
  </code></pre>
  
  <p>And here is how I call it on the instance:</p>
  
  <pre><code>instaceOfMyClass.myClosurePropertyWithStringAndAction = ("Done", {
      // do whatever I need to here
  })
  </code></pre>