2017-12-04 10 views
1

iOS 11/Swift 4を使用して、写真ライブラリからモーメントのコレクションをフィルタリングしようとしています。私はちょうどPlacesがほしいと思う。 nilオプションを使用してモーメントのコレクションを取得すると、13の瞬間が得られ、8つの瞬間は非ゼロのタイトル(位置)を持ちます。私が単純な述語localizedTitle != nilでフェッチのモーメントを行うと、結果は常にnilになります。空のString ("")をnilに代入すると、空の結果も得られます。コンソール上のPHAssetCollectionのモーメントのフィルタリング

let momentOptions = PHFetchOptions() 
momentOptions.predicate = NSPredicate(format:"localizedTitle != nil") 
momentList = PHAssetCollection.fetchMoments(with: nil) 
let momentListFiltered = PHAssetCollection.fetchMoments(with: momentOptions) 
let assetCount = momentList.count 

for index in 0...assetCount-1 { 
    let a = momentList[index] 
    let sta = a.localizedTitle 
    let stb = a.localizedLocationNames 
    print(index, sta ?? "--", stb) 
} 

結果::

0 -- [] 
1 -- [] 
2 Point Reyes National Seashore ["Point Reyes Station, CA"] 
3 Þingeyjarsveit, Northeast Iceland ["Goðafossvegur"] 
4 Djúpavogshreppur ["East Iceland"] 
5 Rangárþing eystra ["South Iceland"] 
6 New York - Washington Heights ["W 168th St"] 
7 -- [] 
8 -- [] 
9 Jacksonville, NC ["Western Blvd"] 
10 -- [] 
11 Locust Shade Park ["Triangle, VA"] 
12 Piedmont Triad International Airport ["Friendship, NC"] 

(lldb) po momentListFiltered 
<PHFetchResult: 0x60c0002e2100> count=0 

最後に、ちょうど正しい比較確認する:

p momentList[7].localizedTitle == nil 
(Bool) $R2 = true 

答えて

0

ように見えるんここ

は、サンプルコードとコンソールの結果でありますPhotos APIの不具合または文書化されていない制限です。 AppleにBug Reporter経由でバグを提出することをお勧めします。

あなたは、この代替方法が役立つことがあります。

let momentLists = PHCollectionList.fetchMomentLists(with: .momentListCluster, options: nil) 
if momentLists.count > 0 { 
    for index in 0...momentLists.count - 1 { 
     let a = momentLists[index] 
     print(index, a.localizedTitle ?? "--", a.localizedLocationNames) 
    } 
} else { 
    print("-- No moment lists! --") 
} 

これは、場所や時間によってグループ写真に思える瞬間クラスタのリストを取得します。

+0

こんにちはDave!ご回答ありがとうございます。私はあなたが示唆するようにバグレポートを提出し、何かが来たら私たちは見るでしょう。私は、私の例のように、コレクションのリストを通常のコレクションよりも使用する利点は実際には感じません。それらはどちらも不変なので、テーブルビューを作成するために、私は乱雑でコレクションから配列を作成し、元のフィルタリングされていないコレクションに戻って、テーブルエントリがタップされたときにアセットを取得する必要があります。より良い方法が必要です! – BlueskyMed

+0

私は、 'PHCollectionList'があなたの後ろにある場所を取得するかもしれないという利点があると思います。' PHAssetCollection'はフィルタリングすることができません。 :)アップルがあなたのバグを認めても、それが最初に修正されるのはiOS 12です。 –

関連する問題