Swift 4 NSPredicate date format not working

536 Views Asked by At

I am trying to fetch photos and videos from the users camera roll, taken within the last 7 days.

Here is my code...

let oneWeekAgoDate = NSCalendar.current.date(byAdding: .weekOfYear, value: -1, to: NSDate() as Date)

fetchOptions.predicate = NSPredicate(format: "date > %@ && (mediaType = %d || mediaType = %d)",oneWeekAgoDate! as NSDate, PHAssetMediaType.image.rawValue, PHAssetMediaType.video.rawValue)

Taken from https://stackoverflow.com/a/30520861/6633865

However my app keeps terminating with this message

Unsupported predicate in fetch options: date > CAST(556405508.551209, "NSDate")

Any ideas?

Thanks

1

There are 1 best solutions below

0
On
 var p: NSPredicate?
 let date = Date()
 p = NSPredicate(format: "mediaType = %d AND ( creationDate < %@ )",PHAssetMediaType.image.rawValue,date as NSDate)

try again!