简体   繁体   English

核心数据“对多”关系将 object 保存在字典中而不是集合中

[英]Core Data 'to-many' relationship save object in a dictionary not a set

I'm working with Core Data and have been using this hackingWithSwift tutorial.我正在使用 Core Data 并一直在使用hackingWithSwift教程。 I have two entities with a one-to-many relationship.我有两个具有一对多关系的实体。

  1. Country Entity国家实体
extension Country {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Country> {
        return NSFetchRequest<Country>(entityName: "Country")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var fullName: String?
    @NSManaged public var shortName: String?
    @NSManaged public var candy: NSSet?
    
    public var wrappedShortName: String {
        shortName ?? "Unknown Country"
    }
    
    public var wrappedFullName: String {
        fullName ?? "Unknown Country"
    }
    
    public var candyArray: [Candy] {
        let set = candy as? Set<Candy> ?? []

        return set.sorted {
            $0.wrappedName < $1.wrappedName
        }
    }

}

// MARK: Generated accessors for candy
extension Country {
    
    @objc(addCandyObject:)
    @NSManaged public func addToCandy(_ value: Candy)

    @objc(removeCandyObject:)
    @NSManaged public func removeFromCandy(_ value: Candy)

    @objc(addCandy:)
    @NSManaged public func addToCandy(_ values: NSSet)

    @objc(removeCandy:)
    @NSManaged public func removeFromCandy(_ values: NSSet)
}
  1. Candy Entity糖果实体
extension Candy {
    
    @nonobjc public class func fetchRequest() -> NSFetchRequest<Candy> {
        return NSFetchRequest<Candy>(entityName: "Candy")
    }
    
    @NSManaged public var id: UUID?
    @NSManaged public var name: String?
    @NSManaged public var origin: Country?
    
    public var wrappedName: String {
        name ?? "Unknown Candy"
    }
}

So Country can have many Candy.所以Country可以有很多Candy。

What I'm wondering is if it is possible that when I create a Candy and save it that rather than being added to the NSSet of Candy's in the Country entity is it possible to save it to a NSDictionary or any other collection type besides NSSet?我想知道的是,当我创建一个 Candy 并保存它而不是添加到 Country 实体中 Candy 的 NSSet 时,是否有可能将它保存到 NSDictionary 或除 NSSet 之外的任何其他集合类型? Not sure if I just change不确定我是否只是改变

    @objc(addCandy:)
    @NSManaged public func addToCandy(_ values: NSSet) //Change to NSDictionary

    @objc(removeCandy:)
    @NSManaged public func removeFromCandy(_ values: NSSet) //Change to NSDictionary

If this works, cause I would like the Entity id property to be the key and the object to be the value.如果这可行,因为我希望 Entity id 属性成为键,而 object 成为值。 Not sure if this is possible.不确定这是否可能。

This is how they are being added这就是它们的添加方式

                    let candy3 = Candy(context: viewContext)
                    candy3.name = "Twix"
                    candy3.orgin = Country(context: viewContext)
                    candy3.orgin?.shortName = "UK"
                    candy3.orgin?.fullName = "United Kingdom"

                    let candy4 = Candy(context: viewContext)
                    candy4.name = "Toblerone"
                    candy4.orgin = Country(context: viewContext)
                    candy4.orgin?.shortName = "CH"
                    candy4.orgin?.fullName = "Switzerland"

                    try? viewContext.save()

Please ask any questions if this doesn't make sense.如果这没有意义,请提出任何问题。

You can use Transformable to encode and decode a Dictionary but you can't change a relationship to a dictionary您可以使用TransformableDictionary进行encodedecode ,但不能更改与字典的关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM