title: Swift function as parameter within a class
body: <p>I'm a swift beginner, so be gentle...</p>
  
  <p>I'm having trouble assigning a function as a parameter.</p>
  
  <p>I have defined this struct:</p>
  
  <pre><code>struct dispatchItem {
     let description: String
     let f: ()-&gt;Void
  
     init(description: String, f: @escaping ()-&gt;()) {
        self.description = description
        self.f = f
     }
  }
  </code></pre>
  
  <p>I make use of this within a class called <code>MasterDispatchController</code> like so:</p>
  
  <pre><code>class MasterDispatchController: UITableViewController {
  
     let dispatchItems = [
        dispatchItem(description: "Static Table", f: testStaticTable),
        dispatchItem(description: "Editable Table", f: testEditableTable)
     ]
  
      func testEditableTable() {
          //some code
      }
  
      func testStaticTable() {
          //some code
      }
  </code></pre>
  
  <p>etc.</p>
  
  <p>Then I have a table view in my code that dispatches out to whichever function was clicked on (there are more than just the two I showed in the code above, but that's unimportant) like so</p>
  
  <pre><code>   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dispatchItems[indexPath.row].f()
     }
  </code></pre>
  
  <p>So...  The compiler is not happy with this.  It says when I am defining the dispatchItems let statement: </p>
  
  <blockquote>
    <p>Cannot convert value of type '(MasterDispatchController) -> () -> ()' to expected argument type '() -> ()'</p>
  </blockquote>
  
  <p>I figure... ok... I'm not sure I exactly understand this, but it seems like the compiler wants to know what class the callback functions are going to come from.  I can see why it might need that.  So I kind of blindly follow the pattern the compiler gives me, and change my struct to:</p>
  
  <pre><code>struct dispatchItem {
     let description: String
     let f: (MasterDispatchController)-&gt;()-&gt;Void
  
     init(description: String, f: @escaping (MasterDispatchController)-&gt;()-&gt;()) {
        self.description = description
        self.f = f
     }
  }
  </code></pre>
  
  <p>Great the compiler is happy there, but now when I try to call the function with <code>dispatchItems[indexPath.row].f()</code> it says:</p>
  
  <blockquote>
    <p>Missing parameter #1 in call</p>
  </blockquote>
  
  <p>The function has no parameters, so I got confused...</p>
  
  <p>I thought maybe it was asking me for the instance of the object in question which would make some sense... that would be "self" in my example, so I tried <code>dispatchItems[indexPath.row].f(self)</code> but then I got an error:</p>
  
  <blockquote>
    <p>Expression resolves to an unused function</p>
  </blockquote>
  
  <p>So I'm kind of stuck.</p>
  
  <p>Sorry if this is a stupid question.  Thanks for your help.</p>