NSSortDescriptor 使用实体的 NSDate 属性 (Swift)
NSSortDescriptor using NSDate attribute of Entity (Swift)
我有一个类似于 this one 的问题,但有一点不同。
我想知道当核心数据模型如下时如何在获取请求中 return 特定属性(在本例中为 'weight'):
客户端<-->>评估('Assessment'有权重属性)
这是我的客户 class:
@objc(Client)
class Client: NSManagedObject {
@NSManaged var name: String
@NSManaged var assessment: NSSet
}
我的评价class:
@objc(Assessment)
class Assessment: NSManagedObject {
@NSManaged var weight: Int16
@NSManaged var nsDateOfAssessment: NSDate
@NSManaged var client: Client
}
这是我的提取请求。我知道现在我的 'weights' 数组正在填充托管对象,但我需要的是用 'weight' 属性的整数填充它,并按我的日期排序 'nsDateOfAssessment'如代码注释中所述。然后我依次使用该数组来填充图形。
var client: Client! = nil
var weights = []
func weightFetchRequest() -> NSFetchRequest {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Assessment")
var error: NSError?
//Something for getting only 'weight' attribute of assessment
//Something for sorting 'weights' array by the date attribute 'nsDateOfAssessment' of 'Assessment'
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
if let results = fetchedResults {
weights = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
println(weights)
return fetchRequest
}
如果有更简单的方法,将不胜感激。
您可以通过在 NSFetchRequest 上设置 sortDescriptor 来进行排序。传递 true 或 false 取决于您希望结果是上升还是下降。
var fetchRequest = NSFetchRequest(entityName: "Assessment")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "nsDateOfAssessment", ascending: false)]
然后您可以使用高阶函数映射结果以仅获取您想要的权重属性
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [Assessment]?
请注意我是如何将它设置为您的子类 NSManagedObject 的数组的
if let assessments = fetchedResults {
let weights = assessments.map { assessment in assessment.weight }
}
我有一个类似于 this one 的问题,但有一点不同。
我想知道当核心数据模型如下时如何在获取请求中 return 特定属性(在本例中为 'weight'):
客户端<-->>评估('Assessment'有权重属性)
这是我的客户 class:
@objc(Client)
class Client: NSManagedObject {
@NSManaged var name: String
@NSManaged var assessment: NSSet
}
我的评价class:
@objc(Assessment)
class Assessment: NSManagedObject {
@NSManaged var weight: Int16
@NSManaged var nsDateOfAssessment: NSDate
@NSManaged var client: Client
}
这是我的提取请求。我知道现在我的 'weights' 数组正在填充托管对象,但我需要的是用 'weight' 属性的整数填充它,并按我的日期排序 'nsDateOfAssessment'如代码注释中所述。然后我依次使用该数组来填充图形。
var client: Client! = nil
var weights = []
func weightFetchRequest() -> NSFetchRequest {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Assessment")
var error: NSError?
//Something for getting only 'weight' attribute of assessment
//Something for sorting 'weights' array by the date attribute 'nsDateOfAssessment' of 'Assessment'
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
if let results = fetchedResults {
weights = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
println(weights)
return fetchRequest
}
如果有更简单的方法,将不胜感激。
您可以通过在 NSFetchRequest 上设置 sortDescriptor 来进行排序。传递 true 或 false 取决于您希望结果是上升还是下降。
var fetchRequest = NSFetchRequest(entityName: "Assessment")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "nsDateOfAssessment", ascending: false)]
然后您可以使用高阶函数映射结果以仅获取您想要的权重属性
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [Assessment]?
请注意我是如何将它设置为您的子类 NSManagedObject 的数组的
if let assessments = fetchedResults {
let weights = assessments.map { assessment in assessment.weight }
}