バニラ プロジェクトを作成し、これをアプリ デリゲートの実装にする場合:
@interface TESTAppDelegate ()
@property (nonatomic, strong) NSMetadataQuery *query;
@end
@implementation TESTAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchProgressed:) name:NSMetadataQueryGatheringProgressNotification object:nil];
NSMutableArray *predicates = [@[] mutableCopy];
#define add(format, ...) { \
[predicates addObject:[NSPredicate predicateWithFormat:format, ##__VA_ARGS__]]; \
}
//Toggle which of these lines are commented to experiment with breaking the query
//add(@"kMDItemKind like[c] %@", @"*"); //Works
//add(@"(kMDItemContentType != 'com.apple.mail.emlx.part')"); //Works
//add(@"(kMDItemContentType == 'public.data')"); //Works
//add(@"kMDItemFSName like[c] %@", @"*"); //DOES NOT WORK
add(@"kMDItemFSName like[c] %@", @"*Nashville*"); //works...
self.query = [[NSMetadataQuery alloc] init];
[_query setPredicate:predicates.count > 1? [NSCompoundPredicate andPredicateWithSubpredicates:predicates] : predicates.lastObject];
[_query setSearchScopes:@[[@"~/Downloads" stringByExpandingTildeInPath]]];
NSLog(@"Query %@", [_query startQuery]? @"started" : @"could NOT start!");
}
- (void)searchProgressed:(NSNotification *)note
{
NSLog(@"searchProgressed: %li", _query.resultCount);
}
@end
NSMetadataQuery で "最近" (Lion 以降) 導入された次の非常に異常な動作を確認できるはずです。明らかに機能しなくなりました。
アプリをそのまま実行すると、次のようなログが記録"searchProgressed 1204"
されます。これは、クエリが結果を見つけたことを意味します。しかし、他の述語をコメントアウトしてから実行すると、何も見つかりません。
ワイルドカードまたはプレースホルダーのさまざまな形式を含め、その行の多くのバリエーションを試しました。プレースホルダーを他の形式%K
に変更し、LIKE[c]
もちろん 、 、 などを使用NSMetadataItemFSNameKey
しNSMetadataItemURLKey
ましkMDItemContentType
た。 上記の 1 つの最も単純なケース以外は何も機能しません。
NSMetadataQuery について大きな何かが欠けているに違いありません。NSMetadataQuery は、以前に広く使用して大成功を収めました。