【问题标题】:Request just one attribute of a entity仅请求实体的一个属性
【发布时间】:2015-08-02 15:28:55
【问题描述】:

所以我使用一个核心数据文件和一个名为 BookArray 的单一实体,在该实体内我有四个不同的属性,我想要做的是从实体请求这些属性中的一个而不是全部。是否可以?

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    var request = NSFetchRequest(entityName: "BookArray")
    request.returnsObjectsAsFaults = false
    bookArray = context.executeFetchRequest(request, error: nil)!

假设我有一个名为 sciFi 的属性和另一个名为 Drama,我如何只请求 drama 属性?

【问题讨论】:

    标签: swift core-data attributes entity


    【解决方案1】:

    您可以通过添加:

    request.propertiesToFetch = ["drama"]
    request.resultType = .DictionaryResultType
    

    但是,除非您的其他属性很大,否则不太可能值得:您的 bookArray 将包含一个字典数组,您需要从中解压缩相关值:您也可以直接从数组中执行此操作由正常提取返回的 NSManagedObjects。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2021-05-30
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多