0

I need to add the properties to entity programatically, based on my service response.

For Ex: at the time of creation

class StockEntity : NSObject {
 var Apples : String,
 var Oranges : String
}

After some execution, may be it will add some more properties like, Grapes, Melons.

The newly adding properties are not aware at the initial.

I need to have this values in entity as based on this the calculations will happen. Right now I have done this feature with Dictionary. And I am looking for the most optimal ways.

Bharathi
  • 485
  • 6
  • 16
  • A dictionary sounds like a perfect fit for your use-case. What problems are you having with that approach? – overactor Jun 20 '16 at 09:32
  • Although what you are looking for could propably be accomplished via associated objects ( http://nshipster.com/associated-objects/ ), I strongly advise against this. It introduces a lot of complications and makes your code harder to maintain. – Losiowaty Jun 20 '16 at 09:33
  • You can check this out http://stackoverflow.com/questions/7819092/how-can-i-add-properties-to-an-object-at-runtime – Vishnu gondlekar Jun 20 '16 at 09:38
  • Dictionary or a Collection Type seems perfect match for dynamic sized Homogeneous typed items. – kandelvijaya Jun 20 '16 at 10:10

1 Answers1

0

You can use an struct rather than a class, just like this example:

struct ex {
    var x: String
}
extension ex{
    var y: String{
        get{return "a"}
        set(value){
            y = value
        }
    }
    init(x:String, y:String){
        self.x = x
        self.y = y
    }
}

ex(x: "", y: "")
Norolim
  • 926
  • 2
  • 10
  • 25