title: Swift - Use of unresolved identifier 'self' - from Closure in a Class
body: <p>I am trying to reference a property in my class from a closure declared in my class. I cannot access self from inside my closure, and I'm assuming self would refer to the Class API from within my closure. </p>
  
  <p>I want to declare a closure that I use later as a parameter to pass to a URLSession dataTask (It works without the one error line). I get the error listed in the title. </p>
  
  <blockquote>
    <p>Use of unresolved identifier 'self'</p>
  </blockquote>
  
  <p>I've been writing swift for an entire day now and am just trying things out as a sandbox, so I fully expect some criticism. </p>
  
  <pre><code>class Api {
  
      struct Location {
          var name = String()
          var author = String()
          var averageRating: String?
          var id = Int()
          var lat = Double()
          var lon = Double()
          var type = String()
      }
  
      var locations = [Location]()
  
      var doSomething = {(data: Data?, response: URLResponse?, error: Error?) -&gt; Void in
          if error != nil {
              print(error!.localizedDescription)
          } else {
              do {
                  if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
                      let myResult = json["results"] as! [[String: Any]]
                      var location : Location! = Location()
                      for jsonLocation in myResult {
                          if let name = jsonLocation["name"]{location.name = name as! String}
                          if let author = jsonLocation["author"]{location.author = author as! String}
                          if let id = jsonLocation["id"]{location.id = id as! Int}
                          if let lat = jsonLocation["lat"]{location.lat = lat as! Double}
                          if let lon = jsonLocation["lon"]{location.lon = lon as! Double}
                          if let type = jsonLocation["type"]{location.type = type as! String}
  
                          //ERROR IS HERE, Why does self not reference class API?
                          self.locations.append(location)
                      }
                  }
              } catch {
                  print("error in JSONSerialization")
              }
          }
      }
  }
  </code></pre>
  
  <p>I have found <a href="https://stackoverflow.com/questions/29366270/closure-use-unresolved-identifier-self">this</a>, but this example is different so I wasn't sure if it was the same bug or me not understanding swift.</p>