2

I am in the process of learning Swift and have come across an issue that I can't seem to piece together a solution for.

Currently I have two Entities in my data model: Card and Set

Model Graph

A Set can have many cards, but a Card can only belong to one Set.

My cards relationship is set as To Many:

Model Graph

While my set relationship is set to To One:

Model Graph

For these two Entities, I have the following subclass code:

import Foundation
import CoreData

@objc(Set) class Set: NSManagedObject {

    @NSManaged var code: String
    @NSManaged var name: String
    @NSManaged var cards: NSSet

}

extension Set {
    func addCard(value: Card) {
        self.mutableSetValueForKey("cards").addObject(value)
    }

    func getCards() -> [Card] {
        var cards: [Card]
        cards = self.cards.allObjects as [Card]
        return cards
    }
}

import Foundation
import CoreData

@objc(Card) class Card: NSManagedObject {

    @NSManaged var name: String
    @NSManaged var set: Set

}

I have successfully created and verified a Set with code such as this:

var set = NSEntityDescription.insertNewObjectForEntityForName("Set", inManagedObjectContext: context) as Set
set.name = setName;
set.code = setCode;

context.save(nil)

However, later when I attempt to create Card objects and add them to this set I run into an error. Here is the code I am using for that:

// The json data here is already verified as working fine elsewhere in my code, it just serves as the basis for creating the Card objects
var cards: [AnyObject] = json.valueForKey("cards") as NSArray

for var i = 0; i < cards.count; i++ {
    var cardData = cards[i] as NSDictionary

    var card = NSEntityDescription.insertNewObjectForEntityForName("Card", inManagedObjectContext: context) as Card
    card.name = cardData.valueForKey("name") as String

    set.addCard(card)

    context.save(nil)
}

The error being fired currently reads as follows:

2015-01-19 00:25:42.803 <app name>[4667:113927] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet'

I tried tracking the error as close to the point of failure as possible. It seems to happen in the addCard function in the Set extension. I am guessing I have some very minor error in my code, but since I am pretty new to debugging Swift code I am currently at a loss for where to go next. I actually had the assignment of Cards to a Set working previously, but something I changed must have caused this issue to occur.

Leena
  • 2,678
  • 1
  • 30
  • 43
Wade Wojcak
  • 63
  • 1
  • 5
  • 3
    Can you try rename getCards() function in the Set extension, I believe it can cause the issue. – Greg Jan 19 '15 at 09:00
  • 2
    I have seen this error message only in the context of *ordered* to-many relationships: http://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors. – Btw, a much easier way to add a card to a set would be to use the *inverse relationship*: `card.set = set`. – Martin R Jan 19 '15 at 09:04
  • @Greg Yep that solved it. I suppose it was because I was improperly (and unintentionally) overwriting a standard getter function? I wasn't even using the getCards function at the time so I simply commented it out and the error no longer occurs. Thanks! – Wade Wojcak Jan 19 '15 at 09:10
  • @MartinR I was actually doing this but switched to a setter-centric format to see if it would solve my error. Now that I have fixed the issue thanks to Greg, I have switched back to using the inverse relationship. – Wade Wojcak Jan 19 '15 at 09:11
  • @WadeWojcak I added my comment as an answer so maybe someone else will find it helpful. – Greg Jan 19 '15 at 09:14

1 Answers1

4

You should rename getCards() function in the Set extension, I believe core data use this function name and the issue happened because you override it.

Greg
  • 25,317
  • 6
  • 53
  • 62