本文介绍了如何在Swift中分配CoreData多对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSManagedObject类

  class Disease:NSManagedObject {
@NSManaged var diseaseId:String
@NSManaged var diseaseName:String
@NSManaged var dogBreed:NSSet
}

如何向dogBreed添加新关系?如果我将var类型更改为NSMutableSet,更改不会保存到数据库。任何想法?

解决方案

从Xcode 7和Swift 2.0开始, 17583057状态:



  @NSManaged var employees:NSSet 

@NSManaged func addEmployeesObject(employee:Employee)
@NSManaged func removeEmployeesObject(employee:Employee)
@NSManaged func addEmployees(employees:NSSet)
@NSManaged func removeEmployees(employees:NSSet)



所以你只需要声明以下方法,CoreData将处理其余的操作:



@NSManaged func addDogBreedsObject(dogBreed:DogBreed)
@NSManaged func removeDogBreedsObject(dogBreed:DogBreed)
@NSManaged func addDogBreeds :NSSet)
@NSManaged func removeDogBreeds(dogBreeds:NSSet)


I have a NSManagedObject class

class Disease: NSManagedObject {
    @NSManaged var diseaseId: String
    @NSManaged var diseaseName: String
    @NSManaged var dogBreed: NSSet
}

How do I add a new relationship to dogBreed? If i change var type to NSMutableSet, the changes are not saved to database. Any ideas?

解决方案

As of Xcode 7 and Swift 2.0, the release note 17583057 states:

@NSManaged var employees: NSSet

@NSManaged func addEmployeesObject(employee: Employee)
@NSManaged func removeEmployeesObject(employee: Employee)
@NSManaged func addEmployees(employees: NSSet)
@NSManaged func removeEmployees(employees: NSSet)

So you just have to declare the following methods and CoreData will take care of the rest:

@NSManaged func addDogBreedsObject(dogBreed: DogBreed)
@NSManaged func removeDogBreedsObject(dogBreed: DogBreed)
@NSManaged func addDogBreeds(dogBreeds: NSSet)
@NSManaged func removeDogBreeds(dogBreeds: NSSet)

这篇关于如何在Swift中分配CoreData多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:59