[英]Fetch objects having the same value for a given property from CoreData?
I want to fetch persons having the same birthday from CoreData ? 我想从CoreData获取生日相同的人吗?
or in general if I have array of persons [Person]
where Person
class as follow 或一般来说,如果我有很多人[Person]
,则Person
类如下
Person {
var name: String
var birthday: NSDate
}
and I want to group the persons based on birthday not birthdate, so I want [NSDate:[Person]]
dictionary. 并且我想根据生日而不是生日将人分组,所以我想要[NSDate:[Person]]
字典。
so could anyone please help me with this? 所以有人可以帮我吗? Any help is appreciated. 任何帮助表示赞赏。
NSDate
is precise down to the 10th of a millisecond, so first you will have to make sure all dates have the same hours and seconds. NSDate
精确到毫秒的十分之一,因此首先必须确保所有日期都具有相同的小时和秒。
The best way to group in Core Data is with a NSFetchedResultsController
. 分组核心数据的最佳方法是使用NSFetchedResultsController
。 You can use the date as the sectionNameKeyPath
and it will group automatically for you. 您可以将日期用作sectionNameKeyPath
,它将自动为您分组。 The items in the fetched results controller can be easily accessed with index paths, very useful for table views. 可使用索引路径轻松访问获取的结果控制器中的项目,这对于表视图非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.