Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 419
Jordan H Avatar asked Dec 12 '14 03:12

Jordan H


People also ask

How do you write NSPredicate in Swift?

In Swift, constructors skip the "blahWith…" part and just use the class name as a function and then go straight to the arguments, so [NSPredicate predicateWithFormat: …] would become NSPredicate(format: …) . (For another example, [NSArray arrayWithObject: …] would become NSArray(object: …) .

What is NSPredicate in Swift?

A definition of logical conditions for constraining a search for a fetch or for in-memory filtering.


1 Answers

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

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

like image 197
Jordan H Avatar answered Sep 28 '22 09:09

Jordan H