title: Xcode 7/Swift 2.0 XCTestCase waitForExpectationsWithTimeout() EXC_BAD_ACCESS
body: <p>I have had little success practicing test driven development with asynchronous Swift 2.0 code in Xcode 7. The only solutions with which I have had any success are contrived and hacky delay mechanisms that sidestep the need for <code>waitForExepectationsWithTimeout()</code>. I would like to perform asynchronous tests as follows, but this code consistently fails:</p>
  
  <pre><code>import Foundation
  import XCTest
  
  class AsyncTest : XCTestCase {
      func testAsync() {
          let expectation : XCTestExpectation = self.expectationWithDescription("That waitForExpectationsWithTimeout() will actually wait.")
          let queue : dispatch_queue_t = dispatch_queue_create("async", nil);
          dispatch_async(queue, { () -&gt; Void in
              print("Executed!")
              XCTAssert(true, "An aphorism about success.")
              expectation.fulfill()
          })
          waitForExpectationsWithTimeout(100.0, handler: nil) // Arbitrary wait period of 100
      }
  }
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>Thread 1: EXC_BAD_ACCESS(code=1, address=0x6.....) </p>
  </blockquote>
  
  <p>When the expectation is fulfilled (<code>expectation.fulfill()</code>) outside of the asynchronously executed closure, this test will pass as expected (so long as I comment out the fulfillment inside the closure). But doing so obviously defeats the purpose of synchronizing test evaluation.  </p>
  
  <p>I will note that even though the test fails, the <code>Executed!</code> message prints as one would expect. Also, if a breakpoint is introduced on the <code>waitForExpectationsWithTimeout...</code> line, the test succeeds—similarly, the test succeeds when an artificial sleep delay is introduced. This leads me to believe that <code>waitForExepectaionsWithTimeout()</code> is not waiting at all. </p>
  
  <p>Admittedly I am new to Xcode and Swift, so if I am missing something obvious, I would greatly appreciate any feedback. What is wrong with my above code? Are there any environmental variables that I can provide to help debug the problem?</p>
  
  <p>Running: OS X El Capitan 10.11 Beta (15A263e), Xcode 7.0 beta (7A120f)</p>