title: Crash with many-to-many NSManagedObject relationship lookup in Swift 2.0
body: <p>I am trying to implement a many-to-many relationship in Core Data with Swift 2.0 NSManagedObject subclassed, but whenever I try to fetch an object in the relationship I get a strange <code>unrecognized selector sent to class</code> error.</p>
  
  <p>The relationship I am trying to design looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/rd20S.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rd20S.png" alt="enter image description here"></a></p>
  
  <p>Each side of the relationship is unordered, and so should be represented within Core Data as an NSSet. Here are what my class implementations look like:</p>
  
  <p><strong>TaggedContent.swift</strong></p>
  
  <pre><code>import Foundation
  import CoreData
  
  class TaggedContent: NSManagedObject {
      @NSManaged var tags: NSSet?
  }
  </code></pre>
  
  <p><strong>Tag.swift</strong></p>
  
  <pre><code>import Foundation
  import CoreData
  
  class Tag: NSManagedObject {
      @NSManaged var name: String?
      @NSManaged var content: NSSet?
  }
  </code></pre>
  
  <p>When I first launch my app I have no problem populating my data store with data. The problem I have is when I relaunch my app. When I try to look up TaggedContent instances by Tag.name, I continue to run in to some variation of this crash:</p>
  
  <pre><code>Crashed Thread:        0  Dispatch queue: NSManagedObjectContext 0x7fda82f934f0
  
  Exception Type:        EXC_CRASH (SIGABRT)
  Exception Codes:       0x0000000000000000, 0x0000000000000000
  
  Application Specific Information:
  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[CALayer __setObject:forKey:]: unrecognized selector sent to class 0x10426e4a8'
  terminating with uncaught exception of type NSException
  abort() called
  CoreSimulator 179 - Device: iPhone 6 - Runtime: iOS 8.4 (12H141) - DeviceType: iPhone 6
  
  Application Specific Backtrace 1:
  0   CoreFoundation                      0x0000000105e95c65 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x0000000106758bb7 objc_exception_throw + 45
  2   CoreFoundation                      0x0000000105e9cfad +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
  3   CoreFoundation                      0x0000000105df313c ___forwarding___ + 988
  4   CoreFoundation                      0x0000000105df2cd8 _CF_forwarding_prep_0 + 120
  5   CoreData                            0x000000010ea7367c _PFCMT_SetValue + 988
  6   CoreData                            0x000000010ea71644 -[NSManagedObjectContext(_NSInternalAdditions) _retainedObjectWithID:optionalHandler:withInlineStorage:] + 196
  7   CoreData                            0x000000010eab4a9b _PFRetainedObjectIDCore + 331
  8   CoreData                            0x000000010eaa737e -[NSManagedObjectContext(_NestedContextSupport) _parentObjectsForFetchRequest:inContext:error:] + 862
  9   CoreData                            0x000000010eb1bc1d __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 589
  10  CoreData                            0x000000010eaa6fd4 internalBlockToNSManagedObjectContextPerform + 84
  11  libdispatch.dylib                   0x0000000107024964 _dispatch_client_callout + 8
  12  libdispatch.dylib                   0x000000010700dfca _dispatch_barrier_sync_f_invoke + 76
  13  CoreData                            0x000000010eaa6eed _perform + 221
  14  CoreData                            0x000000010eaa6d18 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] + 296
  15  CoreData                            0x000000010ea602ca -[NSManagedObjectContext executeFetchRequest:error:] + 586
  </code></pre>
  
  <p>Notice that Core Data is trying to populate data on a CALayer. I've seen several different object types in these crash logs (such as __NSCFString, __NSCFType, UICollectionViewLayoutAttributes, and __NSArrayM to name a few) and I'm baffled as to why. I keep thinking that I must have missed something, but I can't find anything that I missed. If there's anyone out there who has an idea as to why this would happen I would love to know as I'm currently stuck at this point. </p>
  
  <p><strong>UPDATE</strong>: I was asked to provide the code that leads to the crash. I have a set of helper methods for simplifying fetch requests, but a this simple (and somewhat contrived) example also causes the same crash:</p>
  
  <pre><code>let fetchRequest = NSFetchRequest(entityName: "Tag")
  fetchRequest.predicate = NSPredicate(format: "%K == %@", argumentArray: ["name", "pro"])
  
  var results: [AnyObject] = []
  
  do {
      results = try managedObjectContext.executeFetchRequest(fetchRequest)
  } catch let error {
      NSLog("Error fetching data: \(error)")
  }
  
  if let tags = results as? [Tag] {
      for tag in tags {
          NSLog("Tag: \(tag.name)")
  
          if let contentSet = tag.content {
              for content in contentSet { // Crashes here when trying to access the first member of the set
                  NSLog("    * Content: \(content)")
              }
          }
      }
  }
  </code></pre>