Swift and CoreData, problems with relationship (NSSet) - [NSSet intersectsSet:]: set argument is not an NSSet

前端 未结 2 1903
天涯浪人
天涯浪人 2021-02-19 19:16

I\'ve been trying to get my head around adding objects in relationships using CoreData and Swift. I am at a loss, I do not understand why my code does not work. I am trying to a

2条回答
  •  情深已故
    2021-02-19 20:19

    Yesss!! I found the answer!

    I had created a new function in the class Events.swift (the other side of the relationship).

    I had written the following code:

    import Foundation
    import CoreData
    
    class Event: NSManagedObject {
    
        @NSManaged var timeStamp: NSDate
        @NSManaged var teams: NSSet
    
    }
    
    extension Event {
    
        func addTeamToEvent(team:Teams) {
            var teamz = self.mutableSetValueForKey("teams")
            teamz.addObject(team)
        }
    
        func getNumberOfTeams() -> Int {
            return self.teams.count;
        }
    
        func getTeams() -> [Teams] {
            var tmpsak: [Teams]
            tmpsak = self.teams.allObjects as [Teams]
            tmpsak = self.teams.allObjects as [Teams]
    
            return tmpsak
        }
    
    }
    

    which I thought was unrelated. However, renaming getTeams to getTeamsAsArray removed the problem. I am guessing that CoreData, when filling in the other end of the relationship, uses a built-in function called getTeams() (since the other class was called Teams). I accidentally overrode(?) it, causing it to fail.

    Thank you for your suggestions, and I hope this can be helpful to someone else!

    On a somewhat related note, a bug with similar symptoms was identified a few years ago (and appears to still be present), that shows itself in auto-generated code when using ordered many-to-many relationships.

提交回复
热议问题