В приведенном ниже коде, 25-я строка searchPredicate, не удалось установить правильный запрос. Даже если он верен, он не может получить подходящие метаданные в методе initialGatherComplete, который вызывается уведомлением. Полученные метаданные должны быть отсортированы в соответствии со строкой в _searchField. Пожалуйста, скажите мне, где я ошибся.
// Initialize Search Method
- (void)initiateSearch
{
// Create the metadata query instance. The metadataSearch @property is
// declared as retain
self.metadataSearch=[[[NSMetadataQuery alloc] init] autorelease];
// Register the notifications for batch and completion updates
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryDidUpdate:)
name:NSMetadataQueryDidUpdateNotification
object:_metadataSearch];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(initalGatherComplete:)
name:NSMetadataQueryDidFinishGatheringNotification
object:_metadataSearch];
// Configure the search predicate to find all images using the
// public.image UTI
NSPredicate *searchPredicate;
NSString *searchString = [NSString stringWithFormat:@"*%@*",[_searchField stringValue]];
//Problem is here. Not able to send correct query. Even if correct, not able to get the metadata back sorted according to the search string.
searchPredicate=[NSPredicate predicateWithFormat:@"(kMDItemContentTypeTree == 'public.image' || kMDItemContentTypeTree == 'public.audio' || kMDItemContentTypeTree == 'public.movie') && kMDItemDisplayName == %@",searchString];
[_metadataSearch setPredicate:searchPredicate];
// Set the search scope. In this case it will search the User's home directory
// and the iCloud documents area
NSArray *searchScopes;
searchScopes=[NSArray arrayWithObjects:[kSMMediaSearchPath stringByExpandingTildeInPath],NSMetadataQueryNetworkScope,nil];
[_metadataSearch setSearchScopes:searchScopes];
// Configure the sorting of the results so it will order the results by the
// display name
NSSortDescriptor *sortKeys=[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemDisplayName
ascending:YES] autorelease];
[_metadataSearch setSortDescriptors:[NSArray arrayWithObject:sortKeys]];
// Begin the asynchronous query
[_metadataSearch startQuery];
}
// Method invoked when notifications of content batches have been received
- (void)queryDidUpdate:sender;
{
NSLog(@"A data batch has been received");
}
// Method invoked when the initial query gathering is completed
- (void)initalGatherComplete:sender;
{
// Stop the query, the single pass is completed.
[_metadataSearch stopQuery];
// Process the content. In this case the application simply
// iterates over the content, printing the display name key for
// each image
NSUInteger i=0;
for (i=0; i < [_metadataSearch resultCount]; i++)
{
NSMetadataItem *theResult = [_metadataSearch resultAtIndex:i];
NSString *displayName = [theResult valueForAttribute:(NSString *)kMDItemDisplayName];
NSLog(@"result at %lu - %@",i,displayName);
}
// Remove the notifications to clean up after ourselves.
// Also release the metadataQuery.
// When the Query is removed the query results are also lost.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidUpdateNotification
object:_metadataSearch];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:_metadataSearch];
self.metadataSearch=nil;
}