Coredata关系问题 [英] Coredata relationship issue

查看:92
本文介绍了Coredata关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm facing a weird issue with my core data. I've the following relationship

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

A contact can belong to one category and a category can have multiple contacts. I'd like to access my category in the contact when creating my contact. Is my relationship correct ?

我收到以下错误无法将类型String!的值分配给类型类别的值:
newContact.category = categoryField.text

I'm receiving the following error "cannot assign a value of type 'String!' to a value of type 'Category'" in this field: newContact.category = categoryField.text

这是我尝试访问我的类别字段的方法:

That's how I tried to access my category field:

// 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

}


推荐答案

对象的类别属性是 对象。但是你试图将它设置为一个字符串( categoryField.text )。您可能希望使用 对象,其 name 等于 categoryField.text

The category property of a Contact object is a Category object. But you are trying to set it to be a String (categoryField.text). You probably want to use a Category object whose name is equal to categoryField.text.

您可以创建一个:

    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

但是您可能已经创建了一个 Category name 。所以通常的方法是搜索一个具有正确 name 的类别,如果存在则使用它,或者如果没有则创建一个新类别:

But you might already have created a Category with that name. So the usual approach would be to search for a Category with the right name, use that if it exists, or create a new Category if not:

    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
    }

这篇关于Coredata关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆