I am stuck in a problem with my Core Data model and query on a select that includes dates.
I have some objects in an object with an NSDate attribute; I need to retrieve objects with today's date , but I always get zero from this code:
public func getObjectsOfToday() -> Array<myObject>?
{
let entityDescription = NSEntityDescription.entityForName("Objects", inManagedObjectContext: DataAccess.sharedInstance.managedObjectContext)
let request = NSFetchRequest()
request.entity = entityDescription
request.returnsObjectsAsFaults = false
let today = NSDate()
request.predicate = NSPredicate(format: "(dateStart => %@) AND (dateStart <= %@)", today, today)
var objects: [AnyObject]?
do
{
objects = try DataAccess.sharedInstance.managedObjectContext.executeFetchRequest(request)
}
catch let error as NSError
{
print(error)
objects = nil
}
return objects as? Array<Objects>
}
The problem, I think, is NSPredicate, because it also counts hours, minutes and seconds. If I print today, it is something like:
Printed description of today: 2016-02-28 22:02:01 +0000
but I want the objects to be received with the same date, ignoring hours, minutes and seconds . What should I do?
I also tried to create another NSDate using components:
let components = cal.components([.Day , .Month, .Year ], fromDate: today)
let newDate = cal.dateFromComponents(components)
but the result is the same. What am I doing wrong?