Array of Objects in Core Data Entity?

帅比萌擦擦* 提交于 2020-02-20 04:57:20

问题


I've got two entities, one named exercise and the other named workout. I would like to store several different exercises in each workout object.

What is the correct logic here? Create an array of exercises in each workout?


回答1:


You can't create arrays in Core Data. You can simulate them, however, using To-Many Relationships. Add a relationship to your Workout entity called, say, exercises. Then set its Destination to the Exercise entity and check the "To-Many Relationship" checkbox. Note that the exercises will be stored as an NSSet, not an NSArray, so they will not be ordered. If you want them to be in a specific order, I would recommend using the BWOrderedManagedObject subclass.




回答2:


As the (still perfect) decade old answer of @SteveHarrison explains, you just click for "relationship", but do select "to-many"

so, some entity has many "Reply" items. (I put "CD" in front of all entity names, hence, "CDReply".)

After you next rebuild, for free, you get

hence, say you have one of your CDPost which is p

The code is this simple

for oneJsonReply in yourJson {
    r = CDReply.your code to convert ( oneJsonReply ) to a CDReply
    p.addToReplys( r )


来源:https://stackoverflow.com/questions/2749427/array-of-objects-in-core-data-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!