在实体中提取选定的属性
我有一个具有多个属性的核心数据实体,我想要一个属性中的所有对象的列表。 我的代码如下所示:
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类的描述,但没有多less意义。
任何build议,将不胜感激。
编辑:从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) }
运行时错误消失了,但我仍然不知道它是否工作,因为我不知道如何将列表转换为string列表。
一如既往,build议将不胜感激。
有两种可能性:你可以使用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)") }
Swift 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)") }
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 error as NSError { print("fetch failed: \(error.localizedDescription)") }
第二种方法的优点是只有指定的属性从数据库中获取,而不是整个pipe理对象。
它的缺点是结果不包括托pipe对象上下文中未处理的未保存更改( includesPendingChanges:
在使用.DictionaryResultType
时隐式设置为false
)。