title: Swift, return value from asynchronous block
body: <p>Very new to iOS development and I've just started dabbling in GCD, I've read several articles on SO and have consulted Apple's documentation, however I am struggling to understand how to set a return value from an "expensive" operation.</p>
  
  <p>The below function will loop over sum 100K users (as an example), doing this on the main thread would obviously halt the GUI for several seconds, therefore in my UserList class, I propose to execute this block asynchronously on my its thread:</p>
  
  <pre><code>let queue = dispatch_queue_create("me.alexsims.user_queue", DISPATCH_QUEUE_CONCURRENT)
  
  public func getUserById(userId: String, completionHandler: (result: User) -&gt; ())
  {
      dispatch_async(queue)
      {
          for user in self.users {
              if user.getUserId() == userId
              {
                  completionHandler(result: user)
              }
          }
          // Return an empty user object
          completionHandler(result: User())
      }
  }
  </code></pre>
  
  <p>Now from my understanding, the <code>result</code> variable should be returned and I should then be able to access the <code>User()</code> object which is stored in there from my main thread</p>
  
  <p>However, when I return to my controller and run a test:</p>
  
  <pre><code>var list   = UserList()
  var a_user = User()
  
  a_user = list.getUserByID(userId: "xyz", completionHandler: { (result) -&gt; () in
      println(result)
  })
  </code></pre>
  
  <p>this throws the error <code>Could not find an overload for 'println' that accepts the supplied arguments</code>, I'm guessing thats because I am not on the main queue? So I try:</p>
  
  <pre><code>a_user = list.getUserById(userId: "xyz", completionHandler: { (result) -&gt; () in
      dispatch_async(dispatch_get_main_queue()) {
          println(result)
      }
  })
  </code></pre>
  
  <p>However, I still get the same error. What am I doing wrong here? </p>
  
  <p><strong>SOLVED</strong>
  As Daniel spotted, the problem was providing the optional parameter to the <code>list.getUserByID</code> call.
  After changing it to:</p>
  
  <pre><code>a_user = list.getUserById("abcde", completionHandler: { (result) -&gt; () in
      println(result)
  })
  </code></pre>
  
  <p>The println error vanished.</p>