1

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

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Dan
  • 543
  • 2
  • 13
  • 1
    Are you sure the property `date` is available for the target type of your _fetch_ ? [I can find `creationDate` or `modificationDate`](https://developer.apple.com/documentation/photokit/phasset), but no `date`. – OOPer Aug 26 '18 at 23:07
  • 1
    Unrelated to your issue but use `Calendar` and `Date`, not `NSCalendar` or `NSDate`. – rmaddy Aug 26 '18 at 23:31
  • @OOPer Bingo! That's the page I was looking for thanks! – Dan Aug 26 '18 at 23:35
  • @rmaddy Thanks for the tip. Will do – Dan Aug 26 '18 at 23:35

1 Answers1

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

try again!

Qun Li
  • 1,256
  • 13
  • 13