Search code examples
iosswiftnspredicatephotokitphasset

How to format NSPredicate for PHFetchOptions in Swift?


I cannot figure out the proper format necessary to define a predicate for an instance of PHFetchOptions. I've tried so many combinations but they either do not compile or no assets are returned.

The goal is to get only the photos (no videos) in a given collection:

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "mediaType = %i", "PHAssetMediaType.Image")
assetsFetchResults = PHAsset.fetchAssetsInAssetCollection(assetCollection, options: options)

I've tried all combinations of ==, %d, %@, PHAssetMediaTypeImage, "PHAssetMediaTypeImage", PHAssetMediaType.Image, etc.


Solution

  • options.predicate = NSPredicate(format: "mediaType = %d", PHAssetMediaType.Image.rawValue)
    

    Of course I figure it out, right after posting. ;)