i have a question about how create a relation between 2 NSObjects in Core Data.
First i have this CoreData Structure.

There is a Relationship between files and forms. A form could have multiple files, but one file could only have one form.
I have created the NSObjects only with Swift (so no Bridging Header) - and have now 2 Core Data NSObject Classes:
class Forms: NSManagedObject {
@NSManaged var uuid: String
@NSManaged var damagenumber: String
@NSManaged var internalnumber: String
@NSManaged var date: NSDate
@NSManaged var name: String
@NSManaged var street: String
@NSManaged var postalcode: String
@NSManaged var city: String
@NSManaged var country: String
@NSManaged var templateJSON: String
@NSManaged var formname: String
@NSManaged var files: NSSet
}
class Files: NSManagedObject {
@NSManaged var url: String
@NSManaged var forms: Forms
}
My files are here as a NSSet.
So, i would like for example add a form with 5 files. So startet with something like this:
var request = NSFetchRequest(entityName: "Forms")
request.returnsObjectsAsFaults = false;
let newFormObject = NSEntityDescription.insertNewObjectForEntityForName("Forms", inManagedObjectContext: context) as Forms
newFormObject.setValue(uuid, forKey: "uuid")
......
// insert files
for index in 0...4 {
var request = NSFetchRequest(entityName: "Files")
request.returnsObjectsAsFaults = false;
let newFileObject = NSEntityDescription.insertNewObjectForEntityForName("Files", inManagedObjectContext: context) as Files
newFileObject.setValue(arrFiles[index], forKey: "url")
}
Ok everything works fine. BUT: How do i connect this 2 Objects? If u use Obj-C NSObjects instead of Swift i have a few Methods to add a File to a Form. But how can i do this in Swift only?
And if i have a relation - how do i read all File Objects from a certain Form?
Edit:
i changed now the line:
@NSManaged var files: NSSet
to
@NSManaged var files: [Files]
And using in the loop:
newFormObject.files.append(newFileObject)
And this brings me the following error:
*** ERROR: this process has called an NSArray-taking method, such as initWithArray:, and passed in an NSSet object. This is being worked-around for now, but will soon cause you grief.