Coredata关系问题

我的核心数据面临着一个奇怪的问题。 我有以下关系 在这里输入图像说明

一个联系人可以属于一个类别,一个类别可以有多个联系人。 在创build我的联系人时,我想访问我的联系人分类。 我的关系是正确的吗?

我收到以下错误“无法分配types的值string! 在此字段中input“Category”types的值:newContact.category = categoryField.text

这就是我试图访问我的类别字段:

// MARK: - Properties var contact: Contact? func createCategory() { let entity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!) let category = Category(entity: entity!, insertIntoManagedObjectContext: context) let index = pickerView.selectedRowInComponent(0) let color = categoryColor[index] category.name = name.text category.descript = descriptionField.text category.color = color println(category.name) context?.save(nil) name.text = "" descriptionField.text = "" } func createNewContact() { let entity = NSEntityDescription.entityForName("Contact", inManagedObjectContext: context!) let newContact = Contact(entity: entity!, insertIntoManagedObjectContext: context) newContact.name = nameField.text newContact.email = emailField.text newContact.phone = phoneField.text newContact.category = categoryField.text newContact.photo = UIImageJPEGRepresentation(imageHolder.image, 1) var error: NSError? context?.save(&error) } And that's how my entities look like: @objc (Contact) class Contact: NSManagedObject { @NSManaged var email: String @NSManaged var name: String @NSManaged var phone: String @NSManaged var photo: NSData @NSManaged var category: Category } 

 @objc (Category) class Category: NSManagedObject { @NSManaged var color: AnyObject @NSManaged var descript: String! @NSManaged var name: String! @NSManaged var contact: NSOrderedSet } 

Contact对象的category属性是一个Category对象。 但是你试图把它设置成一个String( categoryField.text )。 您可能想要使用namecategoryField.text相同的Category对象。

你可以创build一个:

  let categoryEntity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!) let newCategory = Category(entity: categoryEntity!, insertIntoManagedObjectContext: context) newCategory.name = categoryField.text // set the other properties for the Category as necessary // then assign the relationship newContact.category = newCategory 

但是您可能已经创build了一个具有该nameCategory 。 因此,通常的做法是searchname正确的类别,如果存在,则使用该类别,否则创build新的类别:

  let categoryEntity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!) let fetchRequest = NSFetchRequest() fetchRequest.entity = categoryEntity! fetchRequest.predicate = NSPredicate(format:"name == %@",categoryField.text) let fetchResults = try? context!.executeFetchRequest(fetchRequest) if let results = fetchResults { var requiredCategory : Category if (results.count > 0) { requiredCategory = results[0] as! Category } else { requiredCategory = Category(entity: categoryEntity!, insertIntoManagedObjectContext: context!) requiredCategory.name = categoryField.text // set the other properties for the Category as necessary } newContact.category = requiredCategory } 

答案是在你的错误:

无法指定“String!”types的值 到“类别”types的值

错误是说,你的category属性在Contact类中定义为保存types对象而不是NSString的实例。

所以你需要分配一个types为Category的对象,如下所示:

创build新的类别,或通过任何方式获取现有的类别,这里我们将创build新的类别并将其分配给联系人。

 let catEntity = NSEntityDescription.entityForName("Category", inManagedObjectContext: context!) let category = Category(entity: catEntity!, insertIntoManagedObjectContext: context) category.name = categoryField.text // get the name from field and set to category property category.color = someColor //assign your required color 

现在将其分配给Contact中的类别属性,如下所示:

 newContact.category = category