title: Empty return value from swift function containing closure
body: <p>I created a function that should return a dictionary filled with data that are retrieved (using json, based on Ray Wenderlich tut) online. That code is in a closure. The problem is that an empty dictionary is returned first, and only afterwards it gets filled. Don't know if this is related to some delay in getting the remote data, but obviously I need the dictionary to be filled first before returning it. Here is the code.</p>
  
  <pre><code>func getStatusFromRemoteSource() -&gt; [StatusModel] {
  
      var statusUpdates = [StatusModel]()
      println("statusUpdates after initialization: \(statusUpdates)") // 1
  
      DataManager.getStatusDataWithSuccess { (statusData) -&gt; Void in
          let json = JSON(data: statusData)
  
          if let jsonArray = json.array {
  
              for jsonItem in jsonArray {
                  var statusVersion: String? = jsonItem["version"].string
                  var statusDescription: String? = jsonItem["message"].string
                  var statusCode: Int? = jsonItem["code"].string!.toInt()
  
                  var update = StatusModel(version: statusVersion, message: statusDescription, code: statusCode)
                  statusUpdates.append(update)
                  println("statusUpdates after appending update: \(statusUpdates)") // 3 (after other function call)
              }
  
              let item = 0
              println("Version \(statusUpdates[item].version) has status \(statusUpdates[item].message)")
              // println("Status code: \(statusUpdates[item].code)")
          }
      }
  
      println("Status updates before return: \(statusUpdates)")   // 2
      return statusUpdates
  }
  </code></pre>
  
  <p>So <code>//1</code> prints first, then <code>//2</code> (still empty) and then the other function (that calls this one) is called. Only then <code>//3</code> is printed (correctly) with the content that should be retrieved.</p>
  
  <p>How can I fill the <code>statusUpdates</code> dictionary before returning it?</p>