一次为多个属性的核心数据setValue [英] Core Data setValue for Multiple Attributes at once

查看:95
本文介绍了一次为多个属性的核心数据setValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Core Data中有一个Book实体,而一个Book类作为模型层。

I have a Book entity in Core Data and a Book class acting as the model layer.

My Book模型有多个属性。在Core Data中保存图书的时候,我想把它们存储起来。

My Book model has multiple attributes. When comes the time to persist the books in Core Data, I would like to store them without having to do

bookObject.setValue(book.title , forKey: "name")
bookObject.setValue(book.author , forKey: "author")
bookObject.setValue(book.datePublished , forKey: "datePublished")
etc...

是否有类似于setValue的方法来同时设置多个CoreData Entity属性? / p>

Is there a method akin to setValue for setting multiple CoreData Entity attributes at once ?

推荐答案

如果 Book 模型类继承从 NSObject 使用
相同的属性名称和类型作为核心数据实体
键值编码:

If the Book model class inherits from NSObject and uses the same property names and types as the Core Data entity then you can use Key-Value Coding:

let keys = ["title", "author", "datePublished"]
let dict = book.dictionaryWithValuesForKeys(keys)
bookObject.setValuesForKeysWithDictionary(dict)

这篇关于一次为多个属性的核心数据setValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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