I'm stuck in a problem with my Core Data model and a fetch request that involves dates.
I have some objects in a entity with a NSDate attribute; I need to extract the objects with the date of today but I always get nil 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 it's the NSPredicate because it considers also hours, minute and seconds. If I print today is something like:
Printing description of today: 2016-02-28 22:02:01 +0000
but I want to fetch objects with just the same date, ignoring hours, minutes and seconds. What I need to 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 it's the same. What am I doing wrong?