gpt4 book ai didi

ios - 核心数据关系问题

转载 作者:行者123 更新时间:2023-11-30 14:02:29 25 4
gpt4 key购买 nike

我的核心数据面临一个奇怪的问题。我有以下关系enter image description here

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

我收到以下错误“无法分配“字符串”类型的值!在此字段中“类别”类型的值: 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 对象。但您试图将其设置为字符串(categoryField.text)。您可能想要使用 name 等于 categoryField.textCategory 对象。

您可以创建一个:

    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

但您可能已经使用该名称创建了一个类别。因此,通常的方法是搜索具有正确名称的类别,如果存在则使用该类别,如果不存在则创建一个新类别:

    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
}

关于ios - 核心数据关系问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32818481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com