获取实体中的选定属性
Posted
技术标签:
【中文标题】获取实体中的选定属性【英文标题】:Fetching selected attribute in entities 【发布时间】:2015-01-20 20:32:37 【问题描述】:我有一个包含多个属性的核心数据实体,我想要一个属性中所有对象的列表。我的代码如下所示:
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let sortDesc = NSSortDescriptor(key: "username", ascending: true)
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
fetchReq.valueForKey("username")
let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context)
userList = context.executeFetchRequest(fetchReq, error: nil) as [Usernames]
但这给了我一个 NSException 错误,我不知道为什么,或者我应该如何做到这一点。我已经阅读了 NSFetchRequest 类的描述,但没有什么意义。
任何建议将不胜感激。
编辑:在收到 Bluehound 的提示后,我将代码更改为:
var userList = [Model]()
@IBAction func printUsers(sender: AnyObject)
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
let sortDesc = NSSortDescriptor(key: "friendID", ascending: true)
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
fetchReq.propertiesToFetch = ["friendID"]
let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context)
userList = context.executeFetchRequest(fetchReq, error: nil) as [Model]
println(userList)
运行时错误消失了,但我仍然不知道它是否有效,因为我不确定如何将列表转换为字符串列表。
一如既往,我们将不胜感激。
【问题讨论】:
使用NSFetchRequest
的propertiesToFetch
属性代替valueForKey
@Bluehound 你能告诉我吗,我在初始化 NSPropertyDescription 时遇到问题。
println(userList) 的输出是什么样的?
@pbasdf [ , , ] 与我获取整个班级时相同。
哦,对不起,把as [Model]
改成as [AnyObject]
。
【参考方案1】:
有两种可能:可以发出正常的fetch请求
并从结果中提取包含所需属性的数组,
使用map()
:
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
var error : NSError?
if let result = context.executeFetchRequest(fetchReq, error: &error) as? [Model]
let friendIDs = map(result) $0.friendID
println(friendIDs)
else
println("fetch failed: \(error!.localizedDescription)")
斯威夫特 2:
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
do
let result = try context.executeFetchRequest(fetchReq) as! [Model]
let friendIDs = result.map $0.friendID
print(friendIDs)
catch let error as NSError
print("fetch failed: \(error.localizedDescription)")
或者您将resultType
设置为.DictionaryResultType
和 propertiesToFetch
到想要的属性。
在这种情况下,获取请求将返回一个字典数组:
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
fetchReq.propertiesToFetch = ["friendID"]
fetchReq.resultType = .DictionaryResultType
var error : NSError?
if let result = context.executeFetchRequest(fetchReq, error: &error) as? [NSDictionary]
let friendIDs = map(result) $0["friendID"] as String
println(friendIDs)
else
println("fetch failed: \(error!.localizedDescription)")
斯威夫特 2:
let fetchReq = NSFetchRequest(entityName: "Identities")
fetchReq.sortDescriptors = [sortDesc]
fetchReq.propertiesToFetch = ["friendID"]
fetchReq.resultType = .DictionaryResultType
do
let result = try context.executeFetchRequest(fetchReq) as! [NSDictionary]
let friendIDs = result.map $0["friendID"] as! String
print(friendIDs)
catch let error as NSError
print("fetch failed: \(error.localizedDescription)")
第二种方法的优点是只有指定的属性 从数据库中获取,而不是从整个托管对象中获取。
它的缺点是结果不包含pending
托管对象上下文中未保存的更改 (includesPendingChanges:
使用 .DictionaryResultType
时隐式设置为 false
。
【讨论】:
我从您问题中的代码中获取了实体名称“Identities”和托管对象子类“Model”的名称。通常情况下,两者都使用相同的名称。 再次感谢 Martin,您永远不会失望 :) 是的,我知道名称不是最佳的,这只是我很久以前创建的“测试项目”。以上是关于获取实体中的选定属性的主要内容,如果未能解决你的问题,请参考以下文章