title: Swift & Couchbase Lite emit function causing EXC_BAD_ACCESS (code = 2)
body: <p>I am a total newbie to Couchbase and Swift and I am trying to get a candidate architecture up and a running for a new app that I am working on. I think Couchbase would be a great fit, except for the fact that I am unable to get it working without crashing. This might be down to a few reasons, but just to sanity check, I just wanted to figure out if it is something I am doing wrong.</p>
  
  <p>The following mapping function is resulting in a EXC_BAD_ACCESS error every time the emit function is called. The function is getting called from a view controller on startup. I have checked the parameters in the debugger and they are are pointing to valid references in memory. Here is the function that creates the query:</p>
  
  <pre><code>  class func listEvents() -&gt; CBLQuery? {
      if let view:CBLView = DatabaseService.sharedInstance.database.viewNamed("events") {
        if !view.mapBlock { //create the map block if it doesn't exist
          func mapBlock(doc: [NSObject: AnyObject]!, emit: ((key: AnyObject, value: AnyObject!) -&gt; ())?) {
            if let type:AnyObject = doc["type"] {
              if type.isKindOfClass(NSString.self) &amp;&amp; (type as String) == Event.docType() {
                if let key: AnyObject = doc["date"] {
                  if let emitFunc = emit {
                    emitFunc(key: key, value: doc)
                  }
                }
              }
            }
          }
          view.setMapBlock(mapBlock,
            reduceBlock: nil,
            version: "1")
        }
        return view.createQuery()
      }
      return nil
    }
  </code></pre>
  
  <p>I have also tried rewriting the entire function in Objective-C and it works, so if nobody can help me, then I am going to put this one down to a bug in the Swift. If it is, I would  like to report the issue to Apple, but my understanding of how the Couchbase Mobile SDK works and Swift is still quite limited. If there is anybody out there that has managed to get Couchbase Lite to work with Swift, it would be great if you could help me out. Or even better, if there is a sample project written in Swift on Github, that would be gold to me at this point.</p>