title: Passing a function in Swift 3
body: <p>One of the drawbacks of a new language is that there often major changes from version to version. As a result anything you find on the Web can easily be out of date. Or maybe it isn't and I just am not reading it correctly.</p>
  
  <p>I'm trying to pass a function in a function call. I can get it to work without any parameters. I just declare the parameter I pass as a class global. Bad experiences in assembler make me want to avoid the dreaded global. Here's the pertinent parts of my working code:</p>
  
  <pre><code>class ViewController: NSViewController {
      var artistData: NSArray = []
  
      override func viewDidLoad() {
  /*1*/   getDataFromUrl(url: "http://catbox.loc/artists.json", loadFunction: loadArtistTable())
        //getDataFromUrl(url: "http://catbox.loc/artists.json", loadFunction: loadArtistTable)
      }
  
  /*2*/func getDataFromUrl(url: String, loadFunction: ()) {
    //func getDataFromUrl(url: String, loadFunction: (NSArray)) {
    session.dataTask(with: request) { data, response, err in
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray
            self.artistData = json!
            }
            catch {
                print(error)
            }
  /*3*/     loadFunction
          //loadFunction(self.artistData)
        }.resume()
    }
  /*4*/ func loadArtistTable() {
  //func loadArtistTable(_ jsonData: NSArray) {
      ...
    }
  </code></pre>
  
  <p>The commented out code is one of my many permutations on trying to get it to compile. Ideally, I would like to pass 'json' from the JSONSerialization call. This last attempt was base on the Apple documentation for closure where they gave the example:</p>
  
  <pre><code>var reversedNames = names.sorted(by: backward)
  </code></pre>
  
  <h2>My Answer</h2>
  
  <p>I've accepted @Nirav D's answer, but I didn't try it. My answer is based on his code. I decided to stick with NSArray. The numbers correspond to the ones I've added to the code above.</p>
  
  <pre><code>/*1*/ getDataFromUrl(url: "http://catbox.loc/records.json", loadFunction: loadArtistTable)
  /*2*/ func getDataFromUrl(url: String, loadFunction: @escaping (NSArray) -&gt;()) {
  /*3*/ loadFunction(json)
  /*4*/ func loadArtistTable(jsonData: NSArray) {
  </code></pre>
  
  <p>In 3, I'm using the output of the JSONSerialization directly instead of copying it to a class variable.</p>
  
  <h2>Update</h2>
  
  <p>I tried @Nirav D's method and it works as well.</p>