简体   繁体   English

复制一个结构对象

[英]Copy one struct object

i have to quite similar struct objects. 我必须非常相似的结构对象。 but one includes more values than the other. 但其中一个包含比另一个更多的值。 As the initial is required for KituraKuery methods i can not modify it but require more information for future processing. 由于KituraKuery方法要求使用缩写,因此我无法对其进行修改,但需要更多信息以供将来处理。

my problem is now, that these struct objects look like this: 我的问题是,这些结构对象看起来像这样:

struct provider: Codable {
    var firstName: String?
    var lastName: String?
    var email:String?
}
extension provider: Model{
    class Persistence {}
}

struct provider2: Codable {
    var firstName: String?
    var lastName: String?
    var email:String?
    var providerCategories: [categories]?
}
extension provider: Model{
    class Persistence {}
}

what i need is basically a smarter way to copy information from provider to provider2. 我需要的基本上是将信息从提供程序复制到provider2的更智能的方法。

what i did as of now is i provided an init to provider2 taking provider as input and adding all values to it. 到目前为止,我所做的是我为provider2提供了一个初始化,将provider作为输入并将所有值添加到其中。


struct provider2: Codable {
    var firstName: String?
    var lastName: String?
    var email:String?
    var providerCategories: [categories]?

    init(provider: provider?) {
        if let provider = provider{
            firstName = provider.firstName
            lastName = provider.lastName
            email = provider.lastName
        }
    }
extension provider: Model{
    class Persistence {}
}

i however believe this is probably the worst way and there are much better and more lean approaches to it. 但是,我认为这可能是最糟糕的方法,并且有更好,更精益的方法。

I tried myself on protocols but could that not really get to work. 我尝试过使用协议,但确实无法正常工作。

Any input would be great :) 任何输入将是巨大的:)

You could use extensions for this: 您可以为此使用扩展名:

extension provider {
    func asProvider2() -> provider2 {
        return provider2(firstName: firstName, 
                          lastName: lastName, 
                          email: email, 
                          providerCategories: nil)
    }
}

// Then you can use it like this:
let bar = provider(firstName: "foo", lastName: "bar", email: "baz")
let bar2 = bar.asProvider2()

In your approach both Provider and Provider2 struct are tightly coupled to each other. 在您的方法中,Provider和Provider2结构都紧密耦合在一起。 So lets say in future if you want to change Provider struct or you want to initialise Provider2 struct with another struct, then you need to change lot of things. 因此,让我们说说将来是否要更改Provider结构或要使用其他结构初始化Provider2结构,那么您需要进行很多更改。 We can solve the problem easily by decoupling both Provider and Provider2 struct 我们可以通过解耦Provider和Provider2结构轻松解决问题

protocol BasicInfo {
    var firstName: String? { get set }
    var lastName: String? { get set }
    var email:String? { get set }
}

protocol Address {
    var address: String? {get set}
}

struct Provider: BasicInfo {
    var firstName: String?
    var lastName: String?
    var email: String?
}

struct Provider2: BasicInfo, Address {
    var firstName: String?
    var lastName: String?
    var email:String?
    var address: String?

    init(basic: BasicInfo, add: String) {
        firstName = basic.firstName
        lastName = basic.lastName
        email = basic.email
        address = add
    }
}

//Below are instances of respective struct
let provider1 = Provider(firstName: "Test1", lastName: "TestLast1", email: "test1@gmail.com")
var provider2 = Provider2(basic: provider1, add: "Germany")

In above code i have two different Struct Provider and Provider2. 在上面的代码中,我有两个不同的Struct Provider和Provider2。 Provider2 contains more variable than Provider (i have just added a single var to demonstrate). Provider2比Provider包含更多的变量(我刚刚添加了一个var来演示)。 Now lets say in future we don't require Provider to fill Provider2, we have a new struct Provider3 which will fill Provider2 values. 现在让我们说将来我们不需要Provider来填充Provider2,我们有一个新的Provider 3结构来填充Provider2的值。

struct Provider3: BasicInfo {
    var firstName: String?
    var lastName: String?
    var email: String?
    var middleName: String? //new property added
}

//Below are instances of respective struct
let provider3 = Provider3(firstName: "Test1", lastName: "TestLast1", email: "test1@gmail.com")
var provider2 = Provider2(basic: provider3, add: "Germany")

As you see there is no changes in struct Provider2, we have just introduce a new struct, create instance of new struct and passed that instance to Provider2 init method. 如您所见,结构Provider2中没有任何变化,我们只是引入了一个新结构,创建了新结构的实例,并将该实例传递给Provider2的init方法。

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

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