Search code examples
iosswiftcore-data

iOS Swift - How to store array with Core Data?


I'm new to iOS development and was wanting to know which data type I should specify to store multiple strings (array). The app is to do with food and I need to store multiple ingredients as one attribute.

I was thinking of making ingredient as entity, but I just want to make it easy for a starter. I have read about transformable type but people don't seem to recommend using it to store arrays.


Solution

  • Warning: opinionated answer ahead.

    You don't.

    Storing things in an array does not make anything easier for you. On the contrary, it will make things much harder just an hour in. Imagine you want to show all Recipes that contain a selected Ingredient. That wouldn't be easy with your array hack, with a proper model it's only a couple line of code.

    I would recommend to use a good old relationship with a "Join-entity".

    basic Recipe data model

    Yes, this is more complicated than hacking something together that barely works. But it's the correct way.