best data type for 3 level categorising

Disclaimer: I'm completely new to swift/xcode so apologies if I'm not articulating this question well. Here goes...


I'd love some advice as to what would be the best data type for the following.

I have a large amount of images that need levels of categorisation (let's call them category, subCategory and subSubCategory for lack of a better word)


Each category has any number of subCategories and each subCategory has any number of subSubCategories. A subSubCategory can only belong to one subCategory and a subCategory can only belong to one category.


I need to be able to create arrays on the fly of images based on a selected category, subCategory or subSubCategory.

Therefore, I need to be able to access a list of subCategories from a given category etc so the categories need to be defined with the rules above applied (as opposed to just strings arbitrarily assigned) .


I have tried to play around with enums but it doesn't seem to be the best fit or at least a very "hacky" way of attempting to nest them.


Is there an obvious solution I'm missing?


Many thanks in advance

Are the categories known in advance? If so, at compile time, at runtime startup?

Yes, the categories will be defined in the code and not changeable whenthe app is running.

Accepted Answer

I'd suggest you start by trying this approach:


— Create three enums, each of which contains all the categories at the corresponding level (top level categories in one, sub-categories in the second, sub-sub-categories in the third). I'll call these E0, E1 and E2.


— In E2, add a "subCategory" computed property that switches on 'self' (i.e. the sub-sub-category) and returns the corresponding E1 case.


— In E1, add a "subSubCategories" computed property that switches on 'self' (i.e. the sub-category) and returns a set or array of the E2 cases that belong to it.


— Repeat that technique for E0 and E1.


Note that you can makes these enums RawRepresentable based on String, which means you can associate a string with each that's suitable for displaying in your UI (or that can be localized for display, if appropriate).


Now you have type safety between the category levels (you can't use a sub-sub-category where a category is expected, for example). Given a sub-sub-category, you can easily get the sub-category and the category. Given a sub-category, you can easily get a list of what sub-sub-categories to present to the user. Etc.


If necessary for your app, you can also start creating dictionaries that use the three enums as keys. For example, you might have a dictionary keyed on E2 whose corresponding values are sets or arrays of images that have been sub-sub-categorized the same.

Apologies for the delay.


Thank you very much for your thorough solution, going to try it out this weekend.

I haven't used dictionaries before, so will read up about that too - sounds like a good idea.


Many thanks,

Emily

best data type for 3 level categorising
 
 
Q