title: NSFetchRequest could not locate an NSEntityDescription with Swift, Core Data
body: <p>I'm having a problem with Swift throwing an exception when trying to run countForFetchRequest() on one of my Core Data models. I've got a workaround, but it's horrible and I'm hoping there's a better way.</p>
  
  <p>Here's the relevant chunk of the data model:
  <img src="https://i.stack.imgur.com/TeBeI.png" alt="data model screenshot"></p>
  
  <p>Here's the code that throws the exception:</p>
  
  <pre><code>// Returns true if at least one object of type entityName with the value queryValue in the attribute
  // attributeName exists in context context.
  func objectWithAttrValueExists (attributeName: String, queryValue: AnyObject, entityName: NSString, context: NSManagedObjectContext) -&gt; Bool {
      let allEntities = context.persistentStoreCoordinator.managedObjectModel.entitiesByName
      // Some debugging print statements to see what NSEntityDescriptions exist.
      for key in allEntities.keys {
          println(key as String)
      }
      var request = NSFetchRequest(entityName: entityName)
      let attributeFilter = NSPredicate(format: "%K == %@", argumentArray: [attributeName, queryValue])
      request.predicate = attributeFilter
      var error = AutoreleasingUnsafeMutablePointer&lt;NSError?&gt;()
      let count = context.countForFetchRequest(request, error: error) // &lt;- Exception here.
      if count &lt;= 0 {
          return false
      } else {
          return true
      }
  }
  </code></pre>
  
  <p>You can see I've got some debugging print statements in there; the output from them is:</p>
  
  <pre><code>FixtureParameterType
  FixtureParameterCategory
  Fixture
  FixtureParameter
  </code></pre>
  
  <p>And then the exception raised is: <code>NSFetchRequest could not locate an NSEntityDescription for entity name 'Fixture'</code> (but 'Fixture' is getting printed out in the print statement above).</p>
  
  <p>Here's the horrible workaround: I grab the keys from the dictionary of NSEntityDescriptions, iterate through them, and then manually assign the relevant NSEntityDescription to the NSFetchRequest. I did find this SO <a href="https://stackoverflow.com/questions/24440927/swift-core-data-on-ios7-device-entityforname-on-second-entity-is-nil">thread</a> which suggests using an NSString instead of a String, but that doesn't make a difference; I get the same exception either way.</p>
  
  <pre><code>func objectWithAttrValueExists (attributeName: String, queryValue: AnyObject, entityName: String, context: NSManagedObjectContext) -&gt; Bool {
      let allEntities = context.persistentStoreCoordinator.managedObjectModel.entitiesByName
      for key in allEntities.keys {
          println(key as String)
      }
      // I would expect this to work, but it doesn't.
      //var request = NSFetchRequest(entityName: entityName)
      // This works but is horrible.
      var request = NSFetchRequest()
      for key in allEntities.keys {
          let strkey = key as String
          if strkey == entityName {
              request.entity = allEntities[key] as NSEntityDescription
          }
      }
      let attributeFilter = NSPredicate(format: "%K == %@", argumentArray: [attributeName, queryValue])
      request.predicate = attributeFilter
      var error = AutoreleasingUnsafeMutablePointer&lt;NSError?&gt;()
      let count = context.countForFetchRequest(request, error: error) // &lt;- Works!
      if count &lt;= 0 {
          return false
      } else {
          return true
      }
  </code></pre>
  
  <p>I tried doing it this way because I noticed that the values in <code>allEntities</code> are instances of <code>_PFEncodedString</code> which I thought might be screwing up the dictionary hashing. The fact that my workaround works makes me think that's correct, but if that's true then I don't know how anyone else has ever managed to get Core Data working in Swift.</p>
  
  <p>This is driving me nuts -- any help appreciated.</p>