简体   繁体   中英

Swift 4 KeyPath On Objects of Different Type

I have 2 types, A and B that implement the same methods and have the same properties on them. I have defined an extension to fetch a value in a sub property for each of A and B . I want to know if there is a way to reduce those 2 extensions down to 1 method. Imagine there are many more types like A and B so the code duplication problem becomes much worse.

Update: A and B are generated along with many others like them. The original plan is to avoid writing extensions at all for A or B . I don't know if this is possible but I was told I could use KeyPaths for this. The properties names must be different. This is a byproduct of the code generation

struct A {
    var something: Common
}

struct B {
    var somethingElse: Common
}

struct Common {
    var value1: String
    var value2: String
}

extension A {
    func valueFor(condition: Bool) -> String {
      return condition ? self.something.value1 : self.something.value2
    }
}

extension B {
    func valueFor(condition: Bool) -> String {
      return condition ? self.somethingElse.value1 : self.somethingElse.value2
    }
}

I think protocols are the solution for your problem. They help to make code more generic.

protocol CommonContaining {

    var common: Common { get set }

    func valueFor(condition: Bool) -> String {
      return condition ? self.common.value1 : self.common.value2
    }
}


struct A {
    var something: Common
}

struct B {
    var somethingElse: Common
}


extension A: CommonContaining {
     var common: Common {
         return something
    }
}

extension B: CommonContaining {
     var common: Common {
         return somethingElse
    }
}

From what i have understand, if the method is related to Common struct then you should implement this method in the struct itself or to create extension to the struct :

struct A
{
    var something: Common
}

struct B
{
    var somethingElse: Common
}

struct Common
{
    var value1: String
    var value2: String

    func valueFor(condition: Bool) -> String {
    return condition ? self.value1 : self.value2 }
}

var object_1 = A(something: Common(value1: "1", value2: "1"))
var object_2 = B(somethingElse: Common(value1: "1", value2: "2"))

print(object_1.something.valueFor(condition: true))
print(object_2.somethingElse.valueFor(condition: false)) 

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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