在实体中获取所选属性 [英] Fetching selected attribute in entities
问题描述
我有一个核心数据实体具有几个属性,我想要一个属性中的所有对象的列表。我的代码如下所示:
let appDel:AppDelegate = UIApplication.sharedApplication()委托为AppDelegate
: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。
但是这给了我一个NSException-error,这是一个NSException异常,我不知道为什么,或我怎么应该这样做。我已经阅读了NSFetchRequest类的描述,但是没有什么意义。
任何建议都会感激。
编辑:在Bluehound的提示后,我将我的代码更改为:
var userList = [Model] )
@IBAction func printUsers(sender:AnyObject){
let appDel:AppDelegate = UIApplication.sharedApplication()。作为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)
}
运行时错误消失了,但我仍然不知道它是否工作,因为我不知道如何将列表转换为字符串列表。
$ 解决方案有两种可能:你可以发出正常的获取请求
并使用 map()
从结果中提取包含有用属性的数组
:
let fetchReq = NSFetchRequest(entityName:Identities)
fetchReq.sortDescriptors = [sortDesc]
var error: NSError?
如果let result = context.executeFetchRequest(fetchReq,错误:&错误)as? [model] {
let friendIDs = map(result){$ 0.friendID}
println(friendIDs)
} else {
println(fetch failed:\ .localizedDescription))
}
Swift 2: / p>
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错误为NSError {
print(fetch failed:\ error.localizedDescription))
}
resultType 到 .DictionaryResultType
和 propertiesToFetch
在这种情况下,fetch请求将返回一个字典数组:
let fetchReq = NSFetchRequest(entityName: )
fetchReq.sortDescriptors = [sortDesc]
fetchReq.propertiesToFetch = [friendID]
fetchReq.resultType = .DictionaryResultType
var error:NSError?
如果let result = context.executeFetchRequest(fetchReq,错误:&错误)as? [NSDictionary] {
let friendIDs = map(result){$ 0 [friendID] as String}
println(friendIDs)
} else {
println \(error!.localizedDescription))
}
Swift 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错误为NSError {
print(fetch failed:\(error.localizedDescription))
}
第二种方法的优点是只从数据库获取指定的属性
,而不是整个托管对象。
它的缺点是结果不包括待处理的 I have a core-data entity with several attributes, and I want a list of all the objects in one attribute. My code looks like this: But this gives me an NSException-error, and I can't figure out why, or how I'm supposed to do this. I've read the NSFetchRequest class description but couldn't make much sense out of it. Any suggestions would be appreciated. EDIT: After a tip from Bluehound I changed my code to this: The runtime error is gone but I still don't know if it works because I'm not sure how to convert the list to a list of strings. As always, suggestions would be appreciated. There are two possibilities: You can issue a normal fetch request
and extract an array containing the wanted attribute from the result,
using Swift 2: Or you set the Swift 2: The second method has the advantage that only the specified properties
are fetched from the database, not the entire managed objects. It has the disadvantage that the result does not include pending
unsaved changes in the managed object context ( 这篇关于在实体中获取所选属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
托管对象上下文中未保存的更改( includesPendingChanges:$当使用
.DictionaryResultType
)时,c $ c>
隐式设置为 false
b $ b 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]
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)
}
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)")
}
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
to .DictionaryResultType
and propertiesToFetch
to the wanted attribute.
In this case the fetch request will return an array of dictionaries: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)")
}
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)")
}
includesPendingChanges:
is implicitly set to false
when using .DictionaryResultType
).