在CoreData中为每个键保存多个值 [英] Save multiple values per key in CoreData

查看:94
本文介绍了在CoreData中为每个键保存多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究CoreData,并且需要为每个键保存多个值.就我而言,我只需要为CoreData中的单个Key保存几个字符串,最多9个.

具体来说,我想为游戏保留玩家.我的游戏对象已经包含与这些玩家的关系对象,但是我也想将玩家名称保存为游戏对象本身的记录,因为玩家可以被用户删除,并且我希望游戏对象对此不受影响.

我知道在Cloudkit中,您可以将某个键的值设置为例如字符串集",这可以在创建一对多关系时在CoreData关系中完成.我想知道在常规的CoreData键值对中是否也有按书的方法来完成此操作.

解决方案

您可以使用Transformable类型的属性来存储您的集合.NSArray和NSSet符合NSCoding,因此CoreData可以为您处理所有数据转换,存档和取消存档.

I'm currently looking into CoreData and need to save multiple values per key. In my case I just need to save a couple of strings, max 9, for a single Key in CoreData.

Specifically, I want to save players for a game. My game object already contains relationship objects to these players, but I also want to save the Player names as records on the game object itself, as players can be deleted by my users and I want my game objects to be immune for that.

I know in Cloudkit you can set the value of a certain key to e.g. "set of strings", and this can be done in CoreData relationships to when creating a one-to-many relationship. I was wondering if there is a by-the-book way to do this in regular CoreData key-value pairs as well.

解决方案

You could use an attribute of type Transformable to store your set. NSArray and NSSet conform to NSCoding so CoreData can take care of all data transforming, archiving and unarchiving for you.

这篇关于在CoreData中为每个键保存多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆