0

在下面的代码第 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;
}
4

1 回答 1

0

对于初学者,使用 'like' 而不是 '==' 进行字符串比较。更一般地,请阅读Predicate Format String Syntax以获取有关如何编写谓词格式字符串的完整描述。

于 2011-12-29T13:43:23.690 回答