dimanche 22 novembre 2015

Properties and inheritance defined at runtime

I am supposed to create a semantic network.

The tricky part is that the information to create the semantic network will come from a text file which I will not know anything about (size of the semantic network, relationships, names, attributes). I don't need to do any operation with the data, just retrieve it and be able to express its relationship with other nodes.

  • I was considering using regular inheritance but I guess I won't be able to create 'custom' classes with an undefined number of attributes (by the way, all attributes will be handled as Strings).
  • Then I thought plain old tree structures will do it, but it feels a little bit un-Swifty
  • Now I'm thinking of using a dictionary, the keys for the attribute names and an array of strings as values so I can mimic inheritance var attributes = [String: [String]]() This still feels crumby though.

  • I've looked at protocols, extensions and generics but I can't figure a way to use them to solve this. For what I know Protocols are more flexible than classes

Is there a 'standard' way to solve this kind of problems? Is there a way to define objects at runtime? What would be a nice 'swifty' way to implement this?

Thank you

Aucun commentaire:

Enregistrer un commentaire