一尘不染

在实体中获取所选属性

swift

我有一个具有多个属性的核心数据实体,并且想要一个属性中所有对象的列表。我的代码如下所示:

        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)

}

运行时错误消失了,但我仍然不知道它是否有效,因为我不确定如何将列表转换为字符串列表。

一如既往,建议将不胜感激。


阅读 248

收藏
2020-07-07

共1个答案

一尘不染

有两种可能:您可以发出普通的提取请求,并使用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)")
}

第二种方法的优点是,仅从数据库中获取指定的属性,而不是整个托管对象。

这样做的缺点是,结果不包括托管对象上下文中未决的未保存更改(使用时includesPendingChanges:
隐式设置为)。false``.DictionaryResultType

2020-07-07