简体   繁体   English

如何使用另一个结构的列表将结构保存到领域

[英]How to save struct to Realm, with list of another struct

i created data persistent layer in struct according to this guide , but i also need to create some realm object with struct and add it to another Realm object in struct, like array.我根据本指南在结构中创建了数据持久层,但我还需要使用结构创建一些领域对象并将其添加到结构中的另一个领域对象,如数组。 I mean something like that:我的意思是这样的:

public struct Publisher {
    public let identifier: Int
    public let name: String
}

public struct Character {
    public let identifier: Int
    public let name: String
    public let realName: String
    var publisherArray: [Publisher]
}

final class PublisherObject: Object {
    dynamic var identifier = 0
    dynamic var name = ""

    override static func primaryKey() -> String? {
        return "identifier"
    }
}

final class CharacterObject: Object {
    dynamic var identifier = 0
    dynamic var name = ""
    dynamic var realName = ""
    var publisherArray: List<PublisherObject>?

    override static func primaryKey() -> String? {
        return "identifier"
    }
}

In above guide, author added to struct only one another struct, not array.在上面的指南中,作者只添加了另一个结构体,而不是数组。 I got to this point:我到了这一点:

extension Character: Persistable {
    public init(managedObject: CharacterObject) {
        identifier = managedObject.identifier
        name = managedObject.name
        realName = managedObject.realName
        publisherArray = managedObject.publisherArray
            .compactMap(Publisher.init(managedObject:))
    }
}

but i don't know, what i must type here:但我不知道,我必须在这里输入什么:

public func managedObject() -> CharacterObject {
        let character = CharacterObject()
            character.identifier = identifier
            character.name = name
            character.realName = realName
            // here must be something like that, but for array
            // character.publisherArray = publisherArray.managedObject()
            return character
        }

Thanks all of you for help :-)谢谢大家的帮助:-)

Instead of making duplicate classes for your every struct, just use Unrealm which will take care everything for you.不要为您的每个结构制作重复的类,只需使用Unrealm ,它会为您处理一切。 Just make your structs/enums conform from Realmable protocol.只需让您的结构/枚举符合Realmable协议即可。 You can save not only an array of Structs but also dictionaries, optional types, atc..您不仅可以保存结构数组,还可以保存字典、可选类型、atc..

First look:第一眼: 在此处输入图片说明

Here is an example project of how to implement the abstraction layer for the persistency:下面是一个如何实现持久化抽象层的示例项目:

link to github 链接到github

I finally solved my problem :-)我终于解决了我的问题:-)

public func managedObject() -> CharacterObject {
        let character = CharacterObject()
            character.identifier = identifier
            character.name = name
            character.realName = realName
            publisherArray.forEach({ publisher in
                character.publisherArray.append(publisher.managedObject())
            })
            return character
        }

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

相关问题 如何将一个结构数组传递给另一个结构数组? - How to pass a struct array to another struct array? 如何检查结构中是否存在值并将另一个值保存在数组中 - How to check if value exists in struct and save another value in array C如何访问另一个结构中作为结构数组一部分的结构的结构成员? - C How can I acces struct members of a struct that is part of a struct array in another struct? 另一个结构内的结构数组 - struct array inside another struct 如何将 C++ 数组从一个结构复制到另一个结构 - How to copy c++ arrays from one struct into another struct 如何为另一个结构内部的结构数组正确赋值: - How to properly assign values to an Array of struct inside another struct: 如何在另一个动态分配的结构中初始化结构的常量数组 - How to initialize a constant array of struct in another dynamically allocated struct 如何获取属于另一个结构的结构数组(或指针)? 在 C - How to get struct array (or pointer) that is part of another struct? in C 如何在另一个struct arrya中的一个结构中初始化一个int数组? - How to initialize an int array within a struct within another struct arrya? 如何将此具有多个 arrays 的结构复制到另一个临时结构? - How to copy this struct with multiple arrays to another temporary struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM