NSMetaDataQuery, set the setSearchScope paths to be recursive?
In Objective-C, I am setting up an NSMetaDataQuery
and setting the setSearchScope:
for the query to s开发者_JAVA技巧earch a specific users Documents folder. The search works but doesn't recurse down the directory tree, only searching in the Documents folder.
Here is basically what I've attempted, and it works except not searching below the Documents directory:
query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObjects:@"/Users/username/Documents/",nil]];
[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate >= '$time.this_week')"]];
[query startQuery];
I think that the problem is with your NSPredicate format string. (I have been meaning to write this up!) Essentially, your $time.this_week
would work great in a Spotlight query, but not as a NSPredicate string. You have to actually create an NSDate object and pass it into the query string like
[query setPredicate:[NSPredicate predicateWithFormat:@"(kMDItemFSContentChangeDate <= %@)", [NSDate date]]];
Here are the relevant links for your reference:
Comparison of NSPredicate and Spotlight Query Strings
Spotlight Query Expression Syntax
Predicate Format String Syntax
I think your problem might be the trailing slash on /Users/username/Documents/
. When dealing with paths in Objective-C, the framework never puts on a trailing slash. Does taking it off help?
精彩评论