Home > Mobile >  How to convert object into another object
How to convert object into another object

Time:05-24

How i convert an object 1 into object 2 that have additional properties

class Object1 {
   var imageUrl: String
   var title: String
   var description: String
   var order: Int
}

class Object2 {
   var imageUrl: String
   var title: String
   var description: String
   var buttonTitle: String
   var pageType: OnboardingType
}

What i'm trying so far is this

var items: [Object1] = []

var items2: [Object2] = [] {
    return convertItems(items: items)
}

private func convertItems(items: [Object1]) -> [Object2] {
        var object2Item: [Object2] = []

        items.forEach { item in
            if item.order == 1 {
                object2Item.append(Object2(imageUrl: item.imageUrl, title: item.title, description: item.description, buttonTitle: nil, onboardingType: .hajj))
            } else if item.order == 2 {
                object2Item.append(Object2(imageUrl: item.imageUrl, title: item.title, description: item.description, buttonTitle: nil, onboardingType: .hajj))
            } else if item.order == 3 {
                object2Item.append(Object2(imageUrl: item.imageUrl, title: item.title, description: item.description, buttonTitle: "some text".localized, onboardingType: .hajj))
            }
            
        }
      return object2Item
    }

i have seen this in another stackoverflow post with protocol protoco model, but i can't get how to additional properties if i use protocol as an object type

CodePudding user response:

You're question is tagged [oop], so I'll answer from that perspective.

You should avoid treating objects as dumb groups of data, that isn't what OOP is about. Aggregating related fields into records of some kind has predated OOP by decades. OOP is about polymorphism: having objects respond to the same message in different ways, such that all of the users of the objects don't have to manually check its type. I don't see anything in your sample that would benefit much from polymorphism yet.

Objects are intended to model things. I don't know what things you're modelling in this example (Object1 and Object2 don't really communicate much intent, if you see what I mean).

The "correct" OOP solution here is to name your objects after the things they model. If you need to model a transformation from one thing to another, than that transformation should have a name inspired from the business domain. For example, a SalesLead object might turn into a CustomerObject by virtue of a saleCompleted method. Or a DownloadingFile might have a downloadeCompleted method that returns a File. Etc.

If you provide more context to your problem, I can make this suggestion more tailor made, but for now, I would start with something like:

extension Object1 {
    init(from object1: Object) { // FIXME: Give a meaningful name
        let buttonTitle: String?

        switch object1.order {
        case 1, 2: buttonTitle = nil
        case 3: buttonTitle = "some text".localized
        default: fatalError("Not implemented")
        }

        self.init(
            imageUrl: object1.imageUrl,
            title: object1.title,
            description: object1.description,
            buttonTitle: buttonTitle,
            pageType: .hajj,
        )
    }
}

private func convertItems(items: [Object1]) -> [Object2] { // FIXME: Give a meaningful name
    items.map(Object2.init(from:))
}
  • Related